locked
Running script remotely RRS feed

  • Question

  • I have created a script to build a virtual server remotely if in case one of our physical servers goes down for whatever reason. Running the script from the management server works fine but we seem to have issues running the script from a remote PC (Management wants the help desk to run the script a remote PC instead of accessing the management server directly).

    When running the script remotely it stops after renaming the VM and does not execute any other commands in the script.

    Script Excerpt:

    Function Run-SeverBuild {

    #Updating Hosts File
    Write-Host 'Updating Hosts File'
    Invoke-Command -ComputerName $VirtualServerName -ScriptBlock {$File = 'C:\Windows\System32\drivers\etc\hosts'
    $Hosts = Get-Content $File
    $Hosts -replace $Using:CurrentVMNum, $Using:SEVNUM | Set-Content $File
    }

    Import-Module -Name VMware.Powercli
    Connect-VIServer <Server> -user <User> -Password <Password>

    $Division = Read-Host " 06 `n 07 `n 08 `n 21 `n Input 2-digit Division Code"

    $SEVNUM = Read-Host "`n Please Enter Numeric 3-digit number"


    If ($Division -eq "06")
        {
       $PERSDIV = "08"
       $STRDIV = "08-8"
       $CurrentMachineName ="P" + $Division + $SEVNUM 
       $VirtualServerName = "V" + $Division 
       $NewVirtual ="V" + $Division + $SEVNUM 
       If(!$VirtualServerName)
       {Get-VM -Name V000000000 | Set-VM -Name $NewVirtual -Confirm:$false}
       Else 
       {Get-VM -Name V111111111 | Set-VM -Name $NewVirtual -Confirm:$false}
       $CurrentVMNum = $VirtualServerName.Substring(3,3)
       Send-MailMessage -To $Recipients -Cc $CC -from "MGMTSVR@.com" -Body "User $env:USERNAME is converting $CurrentMachineName to Virtual." -SmtpServer smtp.com
       Run-ServerBuild
        }

    The question I guess I am trying to ask: Is there a way to run a script from a remote server that builds a Virtualremotely?

    Monday, October 15, 2018 2:20 PM

All replies

  • You need to post VMWare questions in the VMWare forum.  This is a Microsoft forum.


    \_(ツ)_/

    Monday, October 15, 2018 3:33 PM
  • There are only two commands that do anything with powercli the rest are powershell commands
    Wednesday, October 17, 2018 5:21 PM
  • The command that is failing is a VMWare command.  All of yoru issues are about how to use VMWare commands.

    If you want to ask a question about Hyper-V or about how to use "Write-Host" then OK but this is not a VMWare forum.


    \_(ツ)_/

    Wednesday, October 17, 2018 5:30 PM
  • Ok thanks
    Wednesday, October 17, 2018 5:36 PM
  • There are some syntax and design errors in your script.  I have fixed them.

    Import-Module -Name VMware.Powercli
    $viserver = '<Server>'
    $viuser = '<User>'
    $vipassword = '<Password>'
    
    Function Run-SeverBuild {
        param(
            $VirtualServerName
        )
        Write-Host 'Updating Hosts File'
        Invoke-Command -ComputerName $VirtualServerName -ScriptBlock {
            $File = 'C:\Windows\System32\drivers\etc\hosts'
            $Hosts = Get-Content $File
            $Hosts -replace $Using:CurrentVMNum, $Using:SEVNUM | Set-Content $File
        }
    }
    
    Connect-VIServer $viserver -user $viuser -Password $vipassword
    $Division = Read-Host " 06 `n 07 `n 08 `n 21 `n Input 2-digit Division Code"
    $SEVNUM = Read-Host "`n Please Enter Numeric 3-digit number"
    
    If($Division -eq '06'){
       $PERSDIV = '08'
       $STRDIV = '08-8'
       $CurrentMachineName ='P' + $Division + $SEVNUM 
       $VirtualServerName = 'V' + $Division 
       $NewVirtual ='V' + $Division + $SEVNUM 
       If(!$VirtualServerName){
            Get-VM -Name V000000000 | Set-VM -Name $NewVirtual -Confirm:$false
        }else{
            Get-VM -Name V111111111 | Set-VM -Name $NewVirtual -Confirm:$false
        }
       $CurrentVMNum = $VirtualServerName.Substring(3,3)
       Send-MailMessage -To $Recipients -Cc $CC -from MGMTSVR@.com -Body "User $env:USERNAME is converting $CurrentMachineName to Virtual." -SmtpServer smtp.com
       Run-ServerBuild $VirtualServerName
    }
    Even with that you code doesn't make much sense.  You sem to have some ambiguity around what VM you are accessing and renaming.


    \_(ツ)_/

    Wednesday, October 17, 2018 6:55 PM