VMware Cloud Community
nkrugel15
Contributor
Contributor

invoke-vmscript

hi everyone, wonder if someone can point me in the right direction here, i have a script i am using which i run on my vms using the invoke-vmscript command, this script gathers all the participating nodes in the windows cluster and then invokes a command to change the IP address on each of them. The problem is I cant get the double hop to work via invoke-vmscript. IS there specific way t o do this. I have tested my code on a windows machines running the invoke command so i know it works, but as soon as i try it via invoke-vmscript it does nothing

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership

Without the actual code and eventual error messages, there is not a lot we can do I'm afraid.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
nkrugel15
Contributor
Contributor

HI there Lud thanks for the reply, here is what i am trying to do

 

 

$script = {
function Convert-CIDRNetMask {
param(
[Parameter(Mandatory = $true)]
[ValidateRange(0, 32)]
[Int] $CIDR
)
$mask = ([Math]::Pow(2, $CIDR) - 1) * [Math]::Pow(2, (32 - $CIDR))
$bytes = [BitConverter]::GetBytes([UInt32] $mask)
(($bytes.Count - 1)..0 | ForEach-Object { [String] $bytes[$_] }) -join "."
}

function Test-Mask {
param(
[Parameter(Mandatory = $true)]
[String] $MaskString
)
$validBytes = '0|128|192|224|240|248|252|254|255'
$MaskString -match `
('^((({0})\.0\.0\.0)|' -f $validBytes) +
('(255\.({0})\.0\.0)|' -f $validBytes) +
('(255\.255\.({0})\.0)|' -f $validBytes) +
('(255\.255\.255\.({0})))$' -f $validBytes)
}

function Convert-NetMaskCIDR {
param(
[parameter(Mandatory = $true)]
[ValidateScript({Test-Mask $_})]
[String] $MaskString
)
$mask = ([IPAddress] $MaskString).Address
for ( $bitCount = 0; $mask -ne 0; $bitCount++ ) {
$mask = $mask -band ($mask - 1)
}
$bitCount
}


Function Set-ClusIP{
Param([String]$CIDR)
$Subnet = Convert-CIDRNetMask -CIDR $CIDR
$ClusterName = Get-Cluster;
$ClusterName.name
$ip = (Resolve-DnsName $ClusterName | Select IPAddress).IPAddress
$res = Get-ClusterResource | ?{$_.ResourceType -like "IP Address" -and $_.OwnerGroup -like "Cluster Group"}
$param1 = New-Object Microsoft.FailoverClusters.PowerShell.ClusterParameter $res,Address,$ip
$param2 = New-Object Microsoft.FailoverClusters.PowerShell.ClusterParameter $res,SubnetMask,$Subnet
$params = $param1,$param2
$params | Set-ClusterParameter
}


Function Set-InstanceIP{
Param([String]$CIDR)
$Subnet = Convert-CIDRNetMask -CIDR $CIDR
$ints = Get-ClusterResource | ?{$_.Name -like "SQL Server*"} | Get-ClusterParameter -Name InstanceName | select -ExpandProperty Value -Unique
foreach ($int in $ints){
$ip = (Resolve-DnsName $int | Select IPAddress).IPAddress
$res = Get-ClusterResource | ?{$_.ResourceType -like "IP Address" -and $_.OwnerGroup -like "$($int)"}
$param1 = New-Object Microsoft.FailoverClusters.PowerShell.ClusterParameter $res,Address,$ip
$param2 = New-Object Microsoft.FailoverClusters.PowerShell.ClusterParameter $res,SubnetMask,$Subnet
$params = $param1,$param2
$params | Set-ClusterParameter
}
}


$Nodes = (Get-ClusterNode).NodeName


Stop-Cluster -force
$site = $env:USERDNSDOMAIN
foreach ($Node in $Nodes){
$NodeName = (Resolve-DnsName -Name "$($Node+".$Site")" -DnsOnly).Name
$NodeIp = (Resolve-DnsName -Name "$($Node+".$Site")" -DnsOnly).IPAddress
$cim = New-CimSession -ComputerName $Node -Credential $Cred -Authentication Kerberos
Set-NetIPAddress -CimSession $Cim -IPAddress $NodeIp -PrefixLength 23
}


Start-Cluster

$ClusterSvc = Get-Service ClusSvc
if ($ClusterSvc -ne "Running"){
Start-Cluster
}
else {
Set-ClusIP -CIDR 23
}


$Cluster = Get-ClusterResource | ?{$_.ResourceType -like "IP Address" -and $_.OwnerGroup -like "Cluster Group"}
$CMask = $Cluster | Get-ClusterParameter -Name SubnetMask | select -ExpandProperty Value
$CSub = Convert-CIDRNetMask -CIDR 23
if (!($CMask -eq $CSub))
{

Set-ClusIP -CIDR 23
Start-ClusterGroup -Name "Cluster Group"
}

elseif ($Cluster.State -ne "Online") {
Start-ClusterGroup -Name "Cluster Group"
}


Set-InstanceIP -CIDR 23


$Instances = Get-Cluster | Get-ClusterResource | ?{$_.ResourceType -like "IP Address" -and $_.OwnerGroup -notlike "Cluster Group"}
foreach ($instance in $instances){
$IMask = $Instance | Get-ClusterParameter -Name SubnetMask | select -ExpandProperty Value
$ISub = Convert-CIDRNetMask -CIDR 23
if (!($IMask -eq $ISub))
{
Set-InstanceIP -CIDR 23}

elseif ($instance.State -ne "Online") {
$instance = $instance.OwnerGroup.Name
Start-ClusterGroup -Name $Instance
}
}

$i=0
do {
$Resources = Get-ClusterGroup
sleep 1
$i=$i+1
ForEach ($Resource in $Resources)
{
$RC = $Resource.Name
$RCS = $Resource.State
}
}
until (($RCS -ne "OffLine" -and $RCS -ne "Pending") -or ($i -eq 300))

$Instance = Get-ClusterGroup | ?{$_.State -eq "Offline"}
foreach ($int in $instance){
Add-Content -Path \\rsync\reporting\ClusterCheck\InterventionRequired.txt "$($Int) Did not come online in the allotted time. Please manually check"
}
Add-Content -Path \\rsync\reporting\ClusterCheck\Completed.txt "Script Completed"
}

Reply
0 Kudos
LucD
Leadership
Leadership

That is indeed a double hop (Invoke-VMScript + CIM session).

One trick that worked for me is to create a task in the Windows Task Scheduler with Invoke-VMScript  in the target VM.
And let that scheduled task (with your code) run from there.

That should avoid one hop.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
nkrugel15
Contributor
Contributor

ah good idea, and then i could use the invoke-commandas to do it.

 

but what i dont understand is why the vmtools wont even load functions into memory if they have in invoke commands in them 

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure what you mean by that?
Are there any messages?
Anything in the vmware.log on the target VM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tags (1)
Reply
0 Kudos
nkrugel15
Contributor
Contributor

sorry for slow reply been away. vmware have come back saying invoke-vmscript was never written with double hop in mind so cant guarantee it will work, so have decided to call scripts form central location rather than invoke, thanks for all your help LucD

Reply
0 Kudos