$DriveLetter = "N" $UNCPath = "\\192.168.0.1\work" $UserName = "ws2022vm01\administrator" $PasswdFile = "C:\scripts\N_secret.txt" $ScriptFile = "c:\scripts\get-netdrivefreeGB.ps1" $securepasswd = Get-Content $PasswdFile|ConvertTo-SecureString -key (1..16) $cred = New-Object System.Management.Automation.PSCredential $UserName,$securepasswd try { #if (Test-Path $DriveLetter":") { # Remove-PSDrive -Name $DriveLetter -ErrorAction Stop |Out-Null #} if (!(Test-Path $DriveLetter":")) { New-PSDrive -Persist -Name $DriveLetter -PSProvider FileSystem -Root $UNCPath -Credential $cred -ErrorAction Stop |Out-Null } $ret = (powershell.exe $ScriptFile $DriveLetter) #Remove-PSDrive -Name $DriveLetter -ErrorAction Stop |Out-Null return $ret } catch { exit }