That's it, I've had it, I've been working on this for the last 16 hours to no avail and I can't go to sleep until this is done, someone please help me!
I am writing a script to automate the installation of a piece of software on VMs, it uses PowerCLI and PowerShell to connect to multiple VMs sanctimoniously copy to all of them my PowerShell script and run that script. That script mounts a network drive, copies some files over, and runs the installer (later on there is also some verification done). Well it's supposed to mount a network drive, the problem is that it doesn't.
I have tried net use to no avail and even when I hard code something and net use shows the drive on a list I can not access it.
net use z: $global:NetDir /user:domain\$global:username $global:password
This DOES NOT work. I have also tried with hard coded values and while they do work my next function can not access the drive. If you know how to make it work please please let me know.
I have also tried the PowerShell method New-PSDrive
New-PSDrive -Name "z" -PSProvider FileSystem -Root $global:NetDir -Credential $creds -Persist
This also DOES NOT work. Even with hard coded values. The reason is that it gives me an error about there being no parameter with the name "Credentials" or "Presist" even though it clearly does I've tried running it on Windows Server 2008 R2 with all windows updates installed. I have even ran $psversiontable.psversion and $host.version on the server and my local computer, this gives the same error on both PS2 and PS3. Again if anyone has any clue, please, please, please let me know.
Finally, you would think this would be my savoir, the tried and true WScript.Network object but alas this as well fails, and this is for the most part is responsible for my lack of sleep.
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("z:", "$global:NetDir", $false, "domain\$global:username", "$global:password")
Here is the thing about this one, it acts very strangely but if I hard code the values into it I can get it to work but only when manually running my ps1 script on the VM, using Invoke-VMScript does not work, and neither does invoking the ps1 file that runs it, it will run the function and tell me that the credentials are wrong (even though a second before I used them to connect to the network).
I'm at my wit's end and I have no where else to turn to, please help me! Per request I am adding some additional parts of the scripts, the scripts themselves are several hundred lines long and most of them I know work so I wouldn't want to spam this place with thousands of lines of code.
The function to remotely run the script on the vm
function RunScript {
try
{
$Installer = "&`"c:\VM scripts\STAP Install\installer.ps1`""
$global:vmworklist |foreach-object {
Invoke-VMScript -ScriptText ($Installer) -VM "$_" -HostUser "$global:username" -HostPassword "$global:ViPassword" -GuestUser "encore\$global:Eusername" -GuestPassword "$global:Epassword" -confirm -ScriptType PowerShell
}
}
catch
{
write-host "Could not execute the script"
Write-Host "Returned:" $_.Exception.Message
}
}
The function that copies the script over
function CopyScript {
$global:vmworklist |foreach-object {
net use * /delete /y > $null
net use y: \\$_.guard.swg.usma.ibm.com\c /user:encore\$Eusername $Epassword
if (Test-Path "y:\VM scripts")
{
Remove-Item "y:\VM scripts\" -recurse
}
}
try
{
write-host
$global:vmworklist |foreach-object {
Copy-VMGuestFile -source "C:\tools\VM\" -destination "c:\" -VM $_ -HostUser "$global:username" -HostPassword "$global:ViPassword" -GuestUser "encore\$global:EUsername" -GuestPassword "$global:Epassword" -confirm -localtoguest
}
}
catch
{
write-host "Could not copy over the files"
Write-Host "Returned:" $_.Exception.Message
}
}
A little explanation, even though the VMWare API states that Copy-VMGuestFile is supposed to overwrite everything by default, it does not, and there is no switch to enable it, there for I have to check if the files exist and if they do delete them otherwise I get an error and the newest version of the code is not transferred. To achieve that I mount my VM's C drive as a share drive on my local machine which is running this PowerCLI/PowerShell script. When running net use locally it does work and the same applies for when I log into the VM via remote desktop and run my ps1 script there. The issue is that when I run net use on my VM via my powershell script powershell in says it succeeds and if I have it just write out "Net Use" it will show it on the list, however if I go to my computer the drive is not there, and none of my functions can reach it.
You should be able to connect the drive with something like this:
If you can't access the drive after connecting it you need to provide more details (particularly the error you're getting). "Does not work" is an insufficient problem description.