Reputation: 135
I have a script where I am attempting to mount a Windows shared drive with PowerShell, and then I want the same script to be able to access this location to create files/folders within the mounted drive and to move files to the mounted drive. The mounting command appears to work correctly, but the script seems to be unable to access it thereafter. I have tried using the -persistent option, and it did not make a difference. The only other thing I was thinking it could be, is that the command is called within a function.
In the example below, $Domain, $Year, and $Month are all defined further up in my script.
function Mount_Drive {
Clear-Host
Write-Host "Mounting drive..." -NoNewLine -ForegroundColor Yellow
New-PSDrive -Name "M" -Root "\\192.168.1.10\f" -PSProvider "FileSystem" -Credential $S_Creds
Write-Host "Done" -ForegroundColor Green
}
function Make_Dir{
Write-Host "Checking for directory structure..." -NoNewLine -ForegroundColor Yellow
If(Test-Path "M:\$Domain\$Year\$Month"){
Write-Host "[Exists] Done" -ForegroundColor Green
}
Else{
Write-Host "[Does Not Exist]" -ForegroundColor Red
Write-Host "Creating directory structure..." -NoNewLine -ForegroundColor Yellow
##################################
New-Item -Path "M:\$Domain\$Year\$Month" -ItemType "directory" > $Null
Write-Host "Done" -ForegroundColor Green
}
}
Mount_Drive
Make_Dir
The above code will mount the drive, but the Make_Dir function does not work. Likewise, if I use the -persistent option, I cannot see the drive in windows. So I am thinking it is being restricted to only the logic within the function.
If I comment out the mount function, and instead mount the drive through the Windows GUI, the script runs fine.
Another script on a "sister" system runs the same mount commands, but does not use functions; and it works as indented. So I am hoping someone can confirm my theory; as I don't have access to the system I am trying to get this to run on, except on rare occasion. <-- yes I know this is a horrible way to do things, but it is outside of my control :(
Upvotes: 0
Views: 894