1

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.

mrabs
  • 13
  • 6
  • Show your whole script and explain how you're running it (remotely? local? PSRemoting or PSExec?). A single line doesn't give people much to go on. – alroc Aug 28 '13 at 12:35
  • I literally have two scripts one which is ran locally on my machine via PowerCLI and uses the VMware API and another one that runs locally on the VM using straight up powershell. The lines that I posted above are all from that second script. The way I run it is I copy that second script from my local machine over to the VM using the VMWare VPI, and I can confirm the copy works fine, if a bit slow, and then I simply execute the script. There is a command (Invoke-VMScript) to remotely launch applications and scripts, so technically both scripts are ran locally on the respective machines. – mrabs Aug 28 '13 at 13:36
  • The only things that are actually remote are the vSphere management (which works fine) and the executing of the ps1 script in the VM. – mrabs Aug 28 '13 at 13:38
  • [Credentials not working with `New-PSDrive` is a known bug](http://stackoverflow.com/questions/17036912/new-psdrive-does-not-support-credentials/17046111#17046111) with PowerShell v2. I think you have a scoping issue with the variables in your `WScript.Network` method but can't say for sure. – alroc Aug 28 '13 at 13:51
  • All my variables are defined from the get go as global variables and I only call on them as global variables. More then just that no matter where on the script I call the variable they print it. Actually here is another thing that will baffle you: If I print out the variables $global:username and $global:password in plain text BEFORE calling on the WScript.Network Object then it wont give me errors regarding my credentials being wrong, even if it doesn't work. – mrabs Aug 28 '13 at 13:57

2 Answers2

0

You should be able to connect the drive with something like this:

$params = 'use', 'z:', "$global:NetDir", "/user:domain\$global:username",
          "$global:password"
net @params

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • "Does not work" refers to the fact that the drive is not seen by my next command, the error really does just say that "Copy-Item : Cannot find drive. A drive with the name 'z' does not exist. + CategoryInfo : ObjectNotFound: (z:String) [Copy-Item], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell .Commands.CopyItemCommand" I am going to try what you posted right now and see if it works. – mrabs Aug 28 '13 at 12:10
  • net @params returns an error that params is not a recognized cmdlet :( – mrabs Aug 28 '13 at 12:18
  • @MichaelRabinovsky Sorry, I had a typo in the first line. It must be `$params`, not `params`. Please try again. The code should work in PowerShell v2 and v3. – Ansgar Wiechers Aug 28 '13 at 23:13
0

I had faced the same problem. My Invoke-VMScript was not working. After digging deep into it I found my vCenter server(in my case the target server) was having ipv6 configuration. I removed the ipv6 configuration and changed it to ipv4 configuration.

My issue got resolved.

Aatif Akhter
  • 2,126
  • 1
  • 25
  • 46