Mount ISO with a drive letter via Powershell - windows

I am trying to mount an ISO and I would like to associate a drive letter with that mount. I would like to specify the drive letter as say Y:\
The below does not work, can anyone help me out. Thanks
#Variable Declaration
$isoImg = "P:\Software\Windows10CurrentVersion\Windows10.iso"
#Mount ISO and Gather Drive Letter
Mount-DiskImage -ImagePath $isoImg -PassThru | Out-Null
#Obtain Drive Letter
$driveletter = (Get-DiskImage $isoImg | Get-Volume).DriveLetter

You could mount the ISO and accept (for now) the automatically assigned drive letter.
It is possible to change that afterwards, but you will need to run that as Administrator:
# the full path to the ISO file
$isoPath = "P:\Software\Windows10CurrentVersion\Windows10.iso"
# mount the ISO, but first check if it is already mounted
$isoDrive = Get-DiskImage -ImagePath $isoPath
if (!$isoDrive) {
$isoDrive = Mount-DiskImage -ImagePath $isoPath -PassThru
}
# $isoDrive is a Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_DiskImage
# get the DriveLetter currently assigned to the drive (a single [char])
$isoLetter = ($isoDrive | Get-Volume).DriveLetter
Rename the driveletter for this drive.
This part needs to be run as Administrator
# find the volume by its automatically assigned driveletter and set the new drive letter
$drive = Get-WmiObject Win32_Volume -Filter ('DriveLetter = "{0}:"' -f $isoLetter)
$drive.DriveLetter = 'Y:'
$null = $drive.Put()
To dismount the iso drive:
$isoDrive | Dismount-DiskImage | Out-Null
Hope that helps

You can mount the ISO image and then assign the drive letter like this:
# ISO image - replace with path to ISO to be mounted
$isoImg = "P:\Software\Windows10CurrentVersion\Windows10.iso"
# Drive letter - use the required drive letter instead of Y:
$driveLetter = "Y:"
# Mount the ISO, without having a drive letter auto-assigned
$diskImg = Mount-DiskImage -ImagePath $isoImg -NoDriveLetter
# Get mounted ISO volume
$volInfo = $diskImg | Get-Disk | Get-Partition | Get-Volume
# Mount volume with specified drive letter (requires Administrator access)
mountvol $driveLetter $volInfo.UniqueId

Related

How to change network printer name using IP address in windows

Just wanted to post my solution here in case anyone needs it. We recently had to replace a printer that was widely used around the organization. We are using a universal driver for this printer and just needed to update the name of the printer on the machines. Here is the powershell script I came up with.
#Pulls printer name, but it is formatted and not clean. Replace IP with printer IP
$PrinterExtract = wmic printer where "PortName LIKE 'IP%'" GET Name
#removes 'name' string from variable
$PrinterExtract = $PrinterExtract -replace 'Name',''
#removes all empty lines and formatting and creates a new variable
$PrinterName = ($PrinterExtract | Where { $_ -ne "" } | Out-String).Trim()
#pulls printer object
$PrinterObject = Get-Printer -Name $PrinterName
#renames printer. Replace new printer name with the name of new printer
Rename-Printer -InputObject $PrinterObject -NewName "New Printer Name"
I was trying to perform this task without using wmic but could not figure out a way to pull an existing printer's name from an IP. If anyone can provide a more modern solution, it would be appreciated.
Figured it out, if anyone needs it
#Pulls printer name, but it is formatted and not clean
$PrinterExtract = (Get-Printer | Where-Object {$_.PortName -Like "IP"} | Select Name | Where {$_ -ne ""} | Out-String).Trim()
#removes 'name' string from variable
$PrinterExtract = $PrinterExtract -replace 'Name',''
#removes '----' string from variable
$PrinterExtract = $PrinterExtract -replace '----',''
#removes formatting and empty lines
$PrinterName = ($PrinterExtract | Where { $_ -ne "" } | Out-String).Trim()
#pulls printer object into new variable
$PrinterObject = Get-Printer -Name $PrinterName
#renames printer
Rename-Printer -InputObject $PrinterObject -NewName "Printer Name"

How to individually access return values from gwmi in PowerShell?

I want to have a script that copies a file from a USB drive to the system drive (most likely the C: drive).
I need it to get a list of the drives that are of type USB.
Then I need to check which of those drives has the folder containing the files I want to copy.
This is where I am currently at:
# find usb drive letters
$usb = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE
AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}
if (test-path $usb[1]`:\DesiredFolder\file2copy.txt) {
Copy-Item "$usb[1]`:\DesiredFolder\*" -Destination "C:\NewFolder"
}
Here is the response I get when outputting the $usb variable
F: D:
I know it is drive D on this computer, but I want the code to assume the drive letter is unknown so I don't have to worry about knowing which drive letter the USB is on other computers (even if there are multiple USB's plugged in).
How do I access the each drive letter individually to run a search for the folder that contains the files to be copied? Would I run a foreach on the $usb variable?
[Also, if you have any better advice on searching for the file, I'm open to anyone's advice. I was just going to run if (test-path $usb[1]`:\DesiredFolder\file2copy.txt){do stuff} but this is my issue, is the return from gwmi is not an array so I can't index the return value (aka drive letter) that I need to test the path (i.e. $usb[1] does not equal F:). I assume I need to split the results somehow, but when I tried the split function using ":" as delimiter, it gave an error (I assume because split is for strings, and the return from gwmi is some type of object)]
Use ForEach-Object statement to loop over what $usb contains, which will enumerate its elements if it happens to be an array or use it as-is if it is a scalar (single value) - or take no action at all if $usb contains $null:
$usb | ForEach-Object {
if (test-path ${_}:\DesiredFolder\file2copy.txt) {
Copy-Item "${_}:\DesiredFolder\*" -Destination "C:\NewFolder"
}
}
Note how the automatic $_ variable is represented with its name enclosed in {...} (i.e., ${_}) so as to disambiguate it from the : that follows (to prevent the : from being interpreted as a variable's scope specifier / namespace).
This technique is the more robust alternative to escaping the subsequent character with ` (which is what you attempted), as doing so could result in an inadvertent escape sequence.

Removable media holding D drive letter

I have on my system some SD card or removable media drive which by default takes the D drive letter.
I have this:
PS C:\Windows\System32\WindowsPowerShell\v1.0> get-volume | where DriveType -Match "Removable"
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size
----------- --------------- ---------- --------- ------------ ------------- ----
E Removable Healthy 0 B 0 B
And I want to change it to G like this:
PS C:\Windows\System32\WindowsPowerShell\v1.0> get-volume | where DriveType -Match "Removable" |Set-Partition -NewDriveLetter G
But I get:
Set-Partition : One or more parameter values passed to the method were invalid.
At line:1 char:50
+ ... | where DriveType -Match "Removable" |Set-Partition -NewDriveLetter G
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (PS_StorageCmdlets:ROOT/Microsoft/..._StorageCmdlets) [Set-Partition],
CimException
+ FullyQualifiedErrorId : MI RESULT 4,Set-Partition
How can I change this? Or what is the issue? I tried with the get-disk to pass but doesn't work either.
I can change in the computer management gui but I need commandline.
You should be able to do it with something like this.
$DriveLetter = (Get-Volume | Where-Object {$_.DriveType -eq "Removable"}).DriveLetter
Get-Partition -DriveLetter $DriveLetter | Set-Partition -NewDriveLetter G
Removable drives (at least without media) does not work with Set-Partition
Worked around this by scripting diskpart
"SELECT VOLUME D`nREMOVE LETTER=D`nASSIGN LETTER=G NOERR" | diskpart
This selects volume d, removes given letter and assigns new letter (ignoring error)

Powershell Mount Point Automation: Drive letter appears after reboot

I've got a strange behavior with powershell created moint points:
My script creates partitions and mounts them into the file system. After a reboot every partition has a drive letter added.
My script so far:
$disks = Get-Disk|where {$_.Number -ge 5} ## >= 5: index disks
$counter = 1
foreach( $disk in $disks){
$diskName="Disk_"+$counter.ToString("00")
$disk = $disk | initialize-Disk -PartitionStyle GPT -passthru|new-partition -UseMaximumSize
Format-Volume -Partition $disk -FileSystem ReFS -NewFileSystemLabel $diskName -Confirm:$false
New-Item -ItemType Directory -Path ( "f:\Mounts\"+$diskName )
$disk | Add-PartitionAccessPath -AccessPath ( "f:\Mounts\"+$diskName )
}
At first I created the mount point disk as ReFS. This worked well, but the drive letter also appeared after a reboot. Also the mount point wasn't shown in the disk management MMC after the reboot.
Using NTFS fixed the last issue, but the drive letter stil reappears when using the above script.
If I delete the driver letter by hand it doesn't come back.
The system is a Server 2012 R2
Any ideas?
After the tip from Harry Johnston I found a working solution:
$disks = Get-Disk|where {$_.Number -ge 5} ## >= 5: index disks
$counter = 1
foreach( $disk in $disks){
$diskName="Disk_"+$counter.ToString("00")
$disk = $disk | initialize-Disk -PartitionStyle GPT -passthru|new-partition -UseMaximumSize
Format-Volume -Partition $disk -FileSystem ReFS -NewFileSystemLabel $diskName -Confirm:$false
New-Item -ItemType Directory -Path ( "f:\Mounts\"+$diskName )
$disk | Add-PartitionAccessPath -AccessPath ( "f:\Mounts\"+$diskName )
$disk | Set-Partition -NoDefaultDriveLetter $true
}
The last line was added and prevents the operating system in adding a drive letter automatically.

Network drives mapping from XP to 7

I am migrating multiple users from XP to 7 and they all have different mapped drives/locations on their current PC. After copying their all data from old PC to new PC, I am currently manually mapping their drives which consumes lot of time. Is there anyway of automating this process?
Is there any way of running a script on existing XP machine and running the same script on new Win 7 machine to map all the drives?
I am looking for a script or any other way of automating this process.
Thanks.
You could do this for all your users, it would at least tell you what they had.
you'd probably want one central folder, lets say Mappings, so try
net use > \servername\Mappings\%username%_map.txt
Or try something like this
http://www.visualbasicscript.com/List-mapped-drives-on-remote-machine-m28529.aspx
out of boredom i quickly wrote a powershell script to help you out.
Run this on your workstation:
(newpcs and oldpcs must be in correct order so oldpc1 is the old pc of the user of newpc1)
$oldpcs=#("oldpc1", "oldpc2", "oldpc3")
$newpcs = #("newpc1", "newpc2", "newpc3")
$mapping = #{}
for($i=0;$i -lt $oldpcs.Count; $i++){
$mapping.add($oldpcs[$i], $newpcs[$i])
}
foreach ($comp in $oldpcs){
$m = Get-WmiObject win32_systemnetworkconnections -ComputerName $comp
$m | %{
#i know this is not very elegant but whatever
$temp = $_.partcomponent -split "="
$temp = $temp -replace "`"", ""
$temp2= $temp[1] -split " "
$driveletter = $temp2[1] -replace "\(", ""
$driveletter = $driveletter -replace "\)", ""
$path = $temp2[0] -replace "\\\\", "\"
$f = "C:\path\to\folder\" + $mapping.$comp + ".txt"
Add-Content $f "$driveletter;$path"
}
}
Then get the file with corresponding computername to the new computer and run the following:
$txt = Get-Content "C:\path\to\file\$env:computername.txt"
$txt | % {
$temp = $_ -split ";"
net use $temp[0] $temp[1]
}
Remember that you have to run the mapping-script in the context of the user you want to map the drives for
Regards
P.S. Remotely mapping network drives is not possible afaik (i would love to be proven wrong)
You could create a logon script and map it to a user though

Resources