Skip to content

Instantly share code, notes, and snippets.

@Pyromaniaxxx
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pyromaniaxxx/7c68316b4c25ca241669 to your computer and use it in GitHub Desktop.
Save Pyromaniaxxx/7c68316b4c25ca241669 to your computer and use it in GitHub Desktop.
Device NamingとPowerShell Directを組み合わせたテスト
# initialize
[string]$GuestVMName = 'WSTP2_02'
[int]$cnt = 1
$PSList = $null
# Create login credential
$Sstr = ConvertTo-SecureString "password1!" -AsPlainText -Force
$psc = New-Object System.Management.Automation.PsCredential("Administrator", $Sstr)
$Cred = Get-Credential -Credential $psc
(Get-Date).ToString() + " : add network Adapter"
# nic hot add
Add-VMNetworkAdapter -VMName $GuestVMName -SwitchName 'External' -Name $NicName
# set nic device naming
Set-VMNetworkAdapter -VMName $GuestVMName -Name 'TestNIC10' -DeviceNaming On
## 設定した名前が反映されるように再起動
## 製品版では再起動不要になるといいのですが…
## あとお試しで PowerShell Directを利用してゲストOSが起動しているかの判別をしています。
Invoke-Command -VMName $GuestVMName -ScriptBlock {Restart-Computer -Force} -Credential $Cred
(Get-Date).ToString() + " : VM restart"
sleep -Seconds 15
do
{
sleep -Seconds 5
(Get-Date).ToString() + " : OS boot Check $cnt"
$PSList = (Invoke-Command -VMName $GuestVMName -ScriptBlock {Get-Process} -Credential $Cred).count
$cnt ++
}
until ($PSList -ne $null -or $cnt -gt 10)
(Get-Date).ToString() + " : boot vm "
# PowerShell Direct
(Get-Date).ToString() + " : setting ip address"
Invoke-Command -VMName $GuestVMName -ScriptBlock {Get-NetAdapter | ForEach-Object {Disable-NetAdapterBinding -Name $_.Name -ComponentID ms_tcpip6 }} -Credential $Cred
Invoke-Command -VMName $GuestVMName -ScriptBlock {New-NetIPAddress -InterfaceIndex ((Get-NetIPAddress |Where-Object {$_.Interfacealias -eq ((Get-NetAdapterAdvancedProperty | Where-Object {$_.DisplayValue -EQ 'TestNIC10'}).Name)}).InterfaceIndex) -IPAddress "192.168.0.1" -PrefixLength '16'} -Credential $Cred
Invoke-Command -VMName $GuestVMName -ScriptBlock {Set-DnsClientServerAddress -InterfaceIndex ((Get-NetIPAddress |Where-Object {$_.Interfacealias -eq ((Get-NetAdapterAdvancedProperty | Where-Object {$_.DisplayValue -EQ 'TestNIC10'}).Name)}).InterfaceIndex) -ServerAddresses "8.8.8.8,8.8.4.4"} -Credential $Cred
(Get-Date).ToString() + " : setup"
Invoke-Command -VMName $GuestVMName -ScriptBlock {wmic useraccount where "Name='Administrator'" Set PasswordExpires=FALSE} -Credential $Cred
Invoke-Command -VMName $GuestVMName -ScriptBlock {Install-WindowsFeature -Name SNMP-Service,SNMP-WMI-Provider,RSAT-SNMP} -Credential $Cred
(Get-Date).ToString() + " : Setup complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment