File: //var/opt/nydus/ops/customer_local_ops/operating_system/powershell/setNetwork.ps1
Start-Transcript -Path C:\Windows\Temp\setNetwork.log -Append
$endPointAddr=$Args
$taskName="HostingNetworkInitialization"
$taskScript="C:\Windows\Tasks\initializeHostingNetwork.ps1"
$currentIps=Get-NetAdapter -Name Ethernet | Get-NetIPAddress -IPAddress "*"
# Delete HostingNetworkInitialization script, then stop the task
if(Test-Path $taskScript){Remove-Item $taskScript -Force}
schtasks.exe /End /TN $taskName
schtasks.exe /Query /TN $taskName /FO LIST /V
#if only one ip configured
if($endPointAddr.gettype() -eq 'String'){
$endPointAddr=@($endPointAddr)
}
#make sure we have the saved IPs in the IPs to set
'privateip.txt','publicip.txt' | %{
if(Test-Path "C:\Windows\Tasks\$($_)"){
$initialIp=Get-Content "C:\Windows\Tasks\$($_)"
if($endPointAddr -notcontains $initialIp){
$endPointAddr+=@($initialIp)
}
}
}
"Start-Transcript -Path C:\Windows\Temp\initializeHostingNetwork.log" |
Out-File $taskScript -encoding ASCII -append
#compare and modify accordingly
$ipSetCompare=Compare-Object $endPointAddr $($currentIps.IpAddress) -IncludeEqual
$ipSetCompare | %{
$ipaddress=$($_.InputObject)
if($_.SideIndicator -eq '<='){
"Adding $ipaddress"
Get-NetAdapter -Name Ethernet |
New-NetIPAddress -AddressFamily IPv4 -IPAddress $ipaddress -PrefixLength 32 -SkipAsSource $false
"Set-NetIPAddress -IPAddress $ipaddress -SkipAsSource `$False" |
Out-File $taskScript -encoding ASCII -append
}elseif($_.SideIndicator -eq '=>'){
"Removing $ipaddress"
Get-NetIPAddress -IPAddress $_.InputObject | Remove-NetIPAddress -Confirm:$false
}else{
#IP not affected
$ipObject = Get-NetAdapter -Name Ethernet | Get-NetIPAddress | ?{$_.ipaddress -eq $ipaddress}
if($ipObject.PrefixLength -eq 32){
#if there is a public or addon IP, set this to outbound traffic
"Set-NetIPAddress -IPAddress $ipaddress -SkipAsSource `$False" |
Out-File $taskScript -encoding ASCII -append
}else{
#count only ips that will remain
if(($ipSetCompare | ?{$_.SideIndicator -ne '=>'}).count -lt 2){
#if there is only the private ip, set this to outbound traffic
"Set-NetIPAddress -IPAddress $ipaddress -SkipAsSource `$False" |
Out-File $taskScript -encoding ASCII -append
}else{
#otherwise we do not want outbound traffic on the private ip
"Set-NetIPAddress -IPAddress $ipaddress -SkipAsSource `$True" |
Out-File $taskScript -encoding ASCII -append
}
}
}
}
"Get-NetIPAddress -InterfaceAlias Ethernet" |Out-File $taskScript -encoding ASCII -append
"Stop-Transcript" |Out-File $taskScript -encoding ASCII -append
# Run the task script we just wrote
powershell.exe -ExecutionPolicy Unrestricted -File $taskScript
# Log end state for posterity
"Final state:"
Get-NetIPAddress -InterfaceAlias Ethernet
Stop-Transcript