本文整理匯總了Golang中github.com/mitchellh/packer/powershell.PowerShellCmd.Output方法的典型用法代碼示例。如果您正苦於以下問題:Golang PowerShellCmd.Output方法的具體用法?Golang PowerShellCmd.Output怎麽用?Golang PowerShellCmd.Output使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/mitchellh/packer/powershell.PowerShellCmd
的用法示例。
在下文中一共展示了PowerShellCmd.Output方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: verifyPSVersion
func (d *HypervPS4Driver) verifyPSVersion() error {
log.Printf("Enter method: %s", "verifyPSVersion")
// check PS is available and is of proper version
versionCmd := "$host.version.Major"
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(versionCmd)
if err != nil {
return err
}
versionOutput := strings.TrimSpace(string(cmdOut))
log.Printf("%s output: %s", versionCmd, versionOutput)
ver, err := strconv.ParseInt(versionOutput, 10, 32)
if err != nil {
return err
}
if ver < 4 {
err := fmt.Errorf("%s", "Windows PowerShell version 4.0 or higher is expected")
return err
}
return nil
}
示例2: IsRunning
func IsRunning(vmName string) (bool, error) {
var script = `
param([string]$vmName)
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
$vm.State -eq [Microsoft.HyperV.PowerShell.VMState]::Running
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script, vmName)
var isRunning = strings.TrimSpace(cmdOut) == "True"
return isRunning, err
}
示例3: GetVirtualMachineSwitchName
func GetVirtualMachineSwitchName(vmName string) (string, error) {
var script = `
param([string]$vmName)
(Get-VMNetworkAdapter -VMName $vmName).SwitchName
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script, vmName)
if err != nil {
return "", err
}
return strings.TrimSpace(cmdOut), nil
}
示例4: Run
func (s *StepWaitForPowerOff) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
ui.Say("Waiting for vm to be powered down...")
var script powershell.ScriptBuilder
script.WriteLine("param([string]$vmName)")
script.WriteLine("(Get-VM -Name $vmName).State -eq [Microsoft.HyperV.PowerShell.VMState]::Off")
isOffScript := script.String()
for {
powershell := new(powershell.PowerShellCmd)
cmdOut, err := powershell.Output(isOffScript, vmName)
if err != nil {
err := fmt.Errorf("Error checking VM's state: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if cmdOut == "True" {
break
} else {
time.Sleep(time.Second * SleepSeconds)
}
}
return multistep.ActionContinue
}
示例5: mountIntegrationServicesSetupDisk
func (s *StepUpdateIntegrationServices) mountIntegrationServicesSetupDisk(vmName string) (dvdDriveProperties, error) {
var dvdProperties dvdDriveProperties
var script powershell.ScriptBuilder
script.WriteLine("param([string]$vmName)")
script.WriteLine("Add-VMDvdDrive -VMName $vmName")
powershell := new(powershell.PowerShellCmd)
err := powershell.Run(script.String(), vmName)
if err != nil {
return dvdProperties, err
}
script.Reset()
script.WriteLine("param([string]$vmName)")
script.WriteLine("(Get-VMDvdDrive -VMName $vmName | Where-Object {$_.Path -eq $null}).ControllerLocation")
controllerLocation, err := powershell.Output(script.String(), vmName)
if err != nil {
return dvdProperties, err
}
script.Reset()
script.WriteLine("param([string]$vmName)")
script.WriteLine("(Get-VMDvdDrive -VMName $vmName | Where-Object {$_.Path -eq $null}).ControllerNumber")
controllerNumber, err := powershell.Output(script.String(), vmName)
if err != nil {
return dvdProperties, err
}
isoPath := os.Getenv("WINDIR") + "\\system32\\vmguest.iso"
script.Reset()
script.WriteLine("param([string]$vmName,[string]$path,[string]$controllerNumber,[string]$controllerLocation)")
script.WriteLine("Set-VMDvdDrive -VMName $vmName -Path $path -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation")
err = powershell.Run(script.String(), vmName, isoPath, controllerNumber, controllerLocation)
if err != nil {
return dvdProperties, err
}
dvdProperties.ControllerNumber = controllerNumber
dvdProperties.ControllerLocation = controllerLocation
return dvdProperties, err
}
示例6: CreateVirtualSwitch
func CreateVirtualSwitch(switchName string, switchType string) (bool, error) {
var script = `
param([string]$switchName,[string]$switchType)
$switches = Get-VMSwitch -Name $switchName -ErrorAction SilentlyContinue
if ($switches.Count -eq 0) {
New-VMSwitch -Name $switchName -SwitchType $switchType
return $true
}
return $false
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script, switchName, switchType)
var created = strings.TrimSpace(cmdOut) == "True"
return created, err
}
示例7: IpAddress
func IpAddress(mac string) (string, error) {
var script = `
param([string]$mac, [int]$addressIndex)
try {
$ip = Get-Vm | %{$_.NetworkAdapters} | ?{$_.MacAddress -eq $mac} | %{$_.IpAddresses[$addressIndex]}
if($ip -eq $null) {
return ""
}
} catch {
return ""
}
$ip
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script, mac, "0")
return cmdOut, err
}
示例8: Mac
func Mac(vmName string) (string, error) {
var script = `
param([string]$vmName, [int]$adapterIndex)
try {
$adapter = Get-VMNetworkAdapter -VMName $vmName -ErrorAction SilentlyContinue
$mac = $adapter[$adapterIndex].MacAddress
if($mac -eq $null) {
return ""
}
} catch {
return ""
}
$mac
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script, vmName, "0")
return cmdOut, err
}
示例9: GetVirtualMachineNetworkAdapterAddress
func GetVirtualMachineNetworkAdapterAddress(vmName string) (string, error) {
var script = `
param([string]$vmName, [int]$addressIndex)
try {
$adapter = Get-VMNetworkAdapter -VMName $vmName -ErrorAction SilentlyContinue
$ip = $adapter.IPAddresses[$addressIndex]
if($ip -eq $null) {
return $false
}
} catch {
return $false
}
$ip
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script, vmName, "0")
return cmdOut, err
}
示例10: verifyPSHypervModule
func (d *HypervPS4Driver) verifyPSHypervModule() error {
log.Printf("Enter method: %s", "verifyPSHypervModule")
versionCmd := "function foo(){try{ $commands = Get-Command -Module Hyper-V;if($commands.Length -eq 0){return $false} }catch{return $false}; return $true} foo"
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(versionCmd)
if err != nil {
return err
}
res := strings.TrimSpace(string(cmdOut))
if res == "False" {
err := fmt.Errorf("%s", "PS Hyper-V module is not loaded. Make sure Hyper-V feature is on.")
return err
}
return nil
}
示例11: GetHostAdapterIpAddressForSwitch
func GetHostAdapterIpAddressForSwitch(switchName string) (string, error) {
var script = `
param([string]$switchName, [int]$addressIndex)
$HostVMAdapter = Get-VMNetworkAdapter -ManagementOS -SwitchName $switchName
if ($HostVMAdapter){
$HostNetAdapter = Get-NetAdapter | ?{ $_.DeviceID -eq $HostVMAdapter.DeviceId }
if ($HostNetAdapter){
$HostNetAdapterConfiguration = @(get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled = 'TRUE' AND InterfaceIndex=$($HostNetAdapter.ifIndex)")
if ($HostNetAdapterConfiguration){
return $HostNetAdapterConfiguration.IpAddress[$addressIndex]
}
}
}
return $false
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script, switchName, "0")
return cmdOut, err
}
示例12: Run
func (s *StepMountDvdDrive) Run(state multistep.StateBag) multistep.StepAction {
//driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
errorMsg := "Error mounting dvd drive: %s"
vmName := state.Get("vmName").(string)
isoPath := s.RawSingleISOUrl
// Check that there is a virtual dvd drive
var script powershell.ScriptBuilder
powershell := new(powershell.PowerShellCmd)
script.Reset()
script.WriteLine("param([string]$vmName)")
script.WriteLine("(Get-VMDvdDrive -VMName $vmName).ControllerNumber")
controllerNumber, err := powershell.Output(script.String(), vmName)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if controllerNumber == "" {
// Add a virtual dvd drive as there is none
script.Reset()
script.WriteLine("param([string]$vmName)")
script.WriteLine("Add-VMDvdDrive -VMName $vmName")
script.WriteLine("$dvdDrive = Get-VMDvdDrive -VMName $vmName | Select-Object -first 1")
script.WriteLine("Set-VMFirmware -VMName $vmName -FirstBootDevice $dvdDrive")
err = powershell.Run(script.String(), vmName)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
ui.Say("Mounting dvd drive...")
err = hyperv.MountDvdDrive(vmName, isoPath)
if err != nil {
err := fmt.Errorf(errorMsg, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.path = isoPath
return multistep.ActionContinue
}
示例13: GetExternalOnlineVirtualSwitch
func GetExternalOnlineVirtualSwitch() (string, error) {
var script = `
$adapters = Get-NetAdapter -Physical -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq 'Up' } | Sort-Object -Descending -Property Speed
foreach ($adapter in $adapters) {
$switch = Get-VMSwitch -SwitchType External | Where-Object { $_.NetAdapterInterfaceDescription -eq $adapter.InterfaceDescription }
if ($switch -ne $null) {
$switch.Name
break
}
}
`
var ps powershell.PowerShellCmd
cmdOut, err := ps.Output(script)
if err != nil {
return "", err
}
var switchName = strings.TrimSpace(cmdOut)
return switchName, nil
}
示例14: addAndMountDvdDisk
func (s *StepMountSecondaryDvdImages) addAndMountDvdDisk(vmName string, isoPath string) (DvdControllerProperties, error) {
var properties DvdControllerProperties
var script powershell.ScriptBuilder
powershell := new(powershell.PowerShellCmd)
controllerNumber := "0"
if s.Generation < 2 {
// get the controller number that the OS install disk is mounted on
// generation 1 requires dvd to be added to ide controller, generation 2 uses scsi for dvd drives
script.Reset()
script.WriteLine("param([string]$vmName)")
script.WriteLine("$dvdDrives = (Get-VMDvdDrive -VMName $vmName)")
script.WriteLine("$lastControllerNumber = $dvdDrives | Sort-Object ControllerNumber | Select-Object -Last 1 | %{$_.ControllerNumber}")
script.WriteLine("if (!$lastControllerNumber) {")
script.WriteLine(" $lastControllerNumber = 0")
script.WriteLine("} elseif (!$lastControllerNumber -or ($dvdDrives | ?{ $_.ControllerNumber -eq $lastControllerNumber} | measure).count -gt 1) {")
script.WriteLine(" $lastControllerNumber += 1")
script.WriteLine("}")
script.WriteLine("$lastControllerNumber")
controllerNumber, err := powershell.Output(script.String(), vmName)
if err != nil {
return properties, err
}
if controllerNumber != "0" || controllerNumber != "1" {
//There are only 2 ide controllers, try to use the one the hdd is attached too
controllerNumber = "0"
}
}
script.Reset()
script.WriteLine("param([string]$vmName,[int]$controllerNumber)")
script.WriteLine("Add-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber")
err := powershell.Run(script.String(), vmName, controllerNumber)
if err != nil {
return properties, err
}
// we could try to get the controller location and number in one call, but this way we do not
// need to parse the output
script.Reset()
script.WriteLine("param([string]$vmName)")
script.WriteLine("(Get-VMDvdDrive -VMName $vmName | Where-Object {$_.Path -eq $null}).ControllerLocation")
controllerLocation, err := powershell.Output(script.String(), vmName)
if err != nil {
return properties, err
}
script.Reset()
script.WriteLine("param([string]$vmName,[string]$path,[string]$controllerNumber,[string]$controllerLocation)")
script.WriteLine("Set-VMDvdDrive -VMName $vmName -Path $path -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation")
err = powershell.Run(script.String(), vmName, isoPath, controllerNumber, controllerLocation)
if err != nil {
return properties, err
}
log.Println(fmt.Sprintf("ISO %s mounted on DVD controller %v, location %v", isoPath, controllerNumber, controllerLocation))
properties.ControllerNumber = controllerNumber
properties.ControllerLocation = controllerLocation
return properties, nil
}