getting driveletter of mounted iso over network - windows

I have a problem with getting driveletter of a mounted ISO over network. I have used Mount-DiskImage -ImagePath $ISOPATH for mounting.
When i mounted ISO file, which was locally stored on disk D:\, i had no problem with getting the volume information via powershell: Get-DiskImage -ImagePath $ISOPATH | Get-Volume. This is how the script looks like:
Mount-DiskImage –ImagePath $ISOPATH
$driveletter = (Get-DiskImage -ImagePath $ISOPATH | get-volume).driveletter + ":"
cd $driveletter\Nanoserver
Above code works fine with local ISO file.
However, when ISO file was mounted from a network share, the Get-Volume command no longer work. It doesn't see the newly mounted drive, despite the fact that its visible and accessible via windows explorer.
Command Get-DiskImage -ImagePath $ISOPATH | Get-Volume no longer show any information, instead its showing nothing, blank result.
The only way that i see this drive via powershell is by using Get-PSdrive command, however, its hard for me to get the letter in automated way with this command.
Has anybody got solution to this problem?
I gladly provide any more information if required.
Thanks

Workaround (untested in production) based on comparing before ($vl1) and after ($vl2):
$vl1 = #(Get-Volume);
$disk = Mount-DiskImage -ImagePath "\\172.19.21.47\www\debian-8.8.0-amd64-netinst.iso" -PassThru ;
$vl2 = #(Get-Volume);
$dl = #( $vl2 | ? {$vl1 -notcontains $_} )[0].DriveLetter
If you guarantee that no simultaneous disk mounts happen in your system.
Possibly need Start-Sleep after mounting to make sure the drive letter assigned

Related

Logging to network share not working via PS post reboot at startup

Getting ready to use a few scripts that I have had written for a while but was never able to solve one particular issue. Hoping to find a solution.
The project is to do a Win10 Build upgrade to around 900 machines in one weekend. Need to ensure they are back up in time and that the upgrade was successful.
Files are staged on a file share. Scripts are stored on the same share. A task is going to created with a bat file on each machine to go get the scripts and execute them.
In the scripts I have it set to record data and its progress to its own file on the network with the name of the file being computer.txt
The Issue:
After the upgrade finishes rebooting it will execute a task that is set to run 'AtStartup'. I have it reconnect to the network share immediately to start logging its status again. It will sometimes work perfectly fine and other times it will not. 50/50. The script works aside from this. Everything finishes fine.
Why the logging?
There are a lot of computers to watch all at one time with a limited number of people to watch over them and address issues. The logs will let me know where there might be a possible issue with how I have it logging. I would like to continue to get the data post reboots.
It doesn't appear to be a network issue because I have verified the machine is communicating.
Ive tried adding a sleep timer to give Windows 2 minutes to finish booting. That didn't help.
I am not sure where to look to find why it works only sometimes.
Mapping of Network Share Path and setting file variable
$Drive_Root_Path = "\\File Path\"
$Drive_Letter = "X"
Remove-PSDrive $Drive_Letter
$Drive = New-PSDrive -Name $Drive_Letter -PSProvider FileSystem -Root $Drive_Root_Path
$Win10_Upgrade_Log_Folder = $Drive.Root + "Log Folder\"
$Log_File = $Win10_Upgrade_Log_Folder + $env:COMPUTERNAME + ".txt"
Example how data is added to the file
Add-Content -Path $Log_File -Value (Get-Date) -NoNewline
Add-Content -Path $Log_File -Value " Setting Executino Policy back to Restricted."
I know there are better solutions to upgrade Windows but with the options available to me, I had to come up with something. It works minus the part of logging after reboot.
I agree with #Theo to try using a UNC path instead of a mapped drive. I also advocate to try also using fully qualified domain names in the path. This makes your code even simpler:
$Win10_Upgrade_Log_Folder = "\\server.contoso.com\Log Folder\"
$Log_File = $Win10_Upgrade_Log_Folder + $env:COMPUTERNAME + ".txt"
And adding content is the same:
Add-Content -Path $Log_File -Value (Get-Date) -NoNewline
Add-Content -Path $Log_File -Value " Setting Executino Policy back to Restricted."

Cannot copy item using mapped drive in Scheduler

I have a simple PowerShell script that copies a file from a mapped network drive, if it's modified in past 1 day.
$source = "Z:\\"
$target = "E:\target"
$files = get-childitem $source
foreach ($file in $files) {
if($file.LastWriteTime -ge (get-date).AddDays(-1)) {
Copy-Item $file.FullName $target
}
}
This script runs fine if I manually execute it.
If I try to use a scheduled task, the copy does not run. I confirmed the script is running by having it make a directory.
If I instead copy from a local drive instead of a network drive, the script runs fine with a scheduled task.
Schedule Task is running as an Admin Account.
Script copying file from network drive runs fine manually but not via scheduled task. Script runs fine as task if copying from local but not network drive.
Any ideas?
Try specifying the full UNC path rather than a network drive. (Network drives are a per-user configuration item.)
Map the drive as a temporary PowerShell drive...add the following as the first line of the script
New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\sharename

Issues extracting data with PS and saving to UNC path

I'm currently trying to pull msinfo data from a remote server and then save that output onto a share located on another server. When I run the command, a progress bar appears and then completes without apparent issue, but the file isn't saved to the UNC path. I've verified that I have permissions on the share and that the nfo generation itself works. Any ideas?
C:\Windows\system32>msinfo32 /computer servername /nfo \\sharename\filename.nfo
Very Strange, It works on CMD but not with Powershell, Not had the time to explore it, However If you need to run it in powershell you can take this workaround:
$TempFile = [System.IO.Path]::GetTempFileName()
C:\Windows\system32\msinfo32 /computer Computer /nfo $TempFile
Do
{
Sleep 5
}
Until (!(Get-Process msinfo32 -ErrorAction SilentlyContinue))
Copy-Item $TempFile \\Computer\Share\output.nfo
$TempFile | Remove-Item -Force
Got it figured out - I was able to use the switch user param to save the file after pulling it from the server.
Thanks!

Using Powershell FileSystemWatcher on iSeries share

I'm using the following powershell script to monitor new files coming
in an IBM iSeries shared folder.
# variables
#$folder = "\\10.10.0.120\transform\BE\FORM"
#$folder = "C:\Users\Administrator.ALI\Desktop\AS400"
#$folder = "\\nb091002\Temp"
$folder = "I:\"
$filter = "*.txt"
$aswform = "C:\ASWFORM\aswform.exe"
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $folder
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $false
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite -bor [System.IO.NotifyFilters]::FileName
while($TRUE){
$result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::Changed -bor [System.IO.WatcherChangeTypes]::Renamed -bOr [System.IO.WatcherChangeTypes]::Created, 2000);
if($result.TimedOut){
continue;
}
Write-Host $result.Name
#$aswform $folder
}
This seems to work fine on local folders or domain shares.
I've tried mapping the iSeries shared folder to a network drive but it doesn't work.
(10.10.0.120 is the AS400)
I'm pretty sure it has to do something with credentials....
Strange thing is I can access the shared folder from within Windows perfectly.
Does anybody have any clues or tips for me?
PS: little detail, I'll be running this script through task sheduler with this trigger
powershell -NoExit -WindowStyle Hidden -File "C:\ASWFORM\watcher.ps1"
But first I need it working when running the script manually!
I have not been able to get FileSystemWatcher to work unless the target directory was a Windows NTFS drive. If I specify the drive letter of the mapped directory I get
Exception setting "Path": "The directory name W:\ is invalid."
If I use the UNC I get
Exception calling "WaitForChanged" with "2" argument(s): "Error reading the \\\\path.to.my.ibm.i\\root\ directory."
Against a Novell file server I get the directory name is invalid if I use a drive letter. If I use a UNC against a Novell drive it does run, but doesn't detect any changes to the file system. Works fine against a local drive and also against a Windows file server on my network.
I solved the problem by writing a small C# console application to poll the folder
instead of using the .Net FileSystemWatcher object.
I manually (instsrv.exe) installed this program as a service and it seems to be running ok.
If you want the code, please send me a PM and I'll see to it that you get it.

How do I list only the valid mounted disk partitions using Get-PSDrive?

I just discovered PowerShell literally yesterday and I love it.
I am trying to list all the valid mounted partitions so I can make them a variable and run chkdsk on them. The problem is that I don't want to detect backup partitions on the machine. The computer I'm using to write and test this script doesn't have any backup/not-mounted partitions. I am thoroughly reading get-help get-psdrives but I don't have the knowledge to understand if the options can do what I need (or if they already are doing it).
This will get you all the drives that you can run chkdsk on:
Get-PSDrive -PSProvider FileSystem
Edit: This will get you the mounted drives that are either Local Disks (3) or removable disks (2):
Get-WmiObject -class “Win32_LogicalDisk" | ?{ #(2, 3) -contains $_.DriveType }

Resources