繁体   English   中英

Powershell脚本可在ISE中使用,但不能在Power With Powershell中运行

[英]Powershell script works in ISE but not in Run With Powershell

我已经开发了一个脚本,该脚本基本上可以根据选择在删除服务器中重新启动服务。

此选择是通过表单完成的。

问题是...当我使用ISE运行此脚本时,脚本工作正常。

当我在Powershell中使用RIGHT CLICK / RUN运行此程序时

我的表格不起作用。 我创建的按钮没有出现...

有什么事吗

这是我的代码:

Function Write-Centered {
Param(  [string] $message,
        [string] $color = "black")
$offsetvalue = [Math]::Round(([Console]::WindowWidth / 2) + ($message.Length / 2))
Write-Host ("{0,$offsetvalue}" -f $message) -ForegroundColor $color
}

clear

$timestamp=Get-date -format yyyy_MM_dd_hh_mm_ss

$systems = @(
("System 1","Server1","bmc_ctsa_sm_SAP_Instance_2"),
("System 2","Server2","bmc_ctsa_sm_SAP_Instance_6"),
("System 3","Server3","bmc_ctsa_sm_SAP_Instance_6")
)

Write-Centered "Service Restart Tool" "Green"
Write-Centered "Choose the target system you would like to restart" "Green"

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(300, 100)
$form1.topmost = $true
$Form1.Controls.Add($Button)
$Form1.Text = "SSPR Restart Tool"

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Point(150, 25)
$Button.Size = New-Object System.Drawing.Size(120, 25)
$Button.add_Click({
    $label.Text = $comboBox1.SelectedItem.ToString()
    $Form1.Close()
    })
$Button.Text = "Start Process"

$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Point(10, 10)
$Label.Size = New-Object System.Drawing.Size(300, 15)
$Label.Text = "Please select the system you would like to restart"

$Form1.Controls.Add($Label)

$comboBox1 = New-Object System.Windows.Forms.ComboBox
$comboBox1.Location = New-Object System.Drawing.Point(10, 25)
$comboBox1.Size = New-Object System.Drawing.Size(120, 25)
foreach($system in $systems)
{
  $comboBox1.Items.add($system[0]) | Out-Null
}
$Form1.Controls.Add($comboBox1)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(70, 90)
$label.Size = New-Object System.Drawing.Size(98, 23)
$label.Text = ""
$Form1.Controls.Add($label)


[void]$form1.showdialog()

Write-Centered $comboBox1.Text "Yellow"

$do=0
$i=0

do {
 if ($comboBox1.Text -eq $systems[$i][0]){
 $result=$i
 $i++
 }
 $do++
}while ($do -le $systems.length-1)

$System=$systems[$result][0]
$Server=$systems[$result][1]
$Instance=$systems[$result][2]


$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
    "Start the process."

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
    "Cancel the process."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice("",`
          "`nWould you like to progress with the service restart? `n`nSystem: $System`nServer: $Server`nInstance: $Instance", $options, 0)

if ($result -eq 0) {
$services = Get-Service -computername $Server -name $Instance
$target=$services.name

$sharefolder = "\\"+$Server+"\c$"
New-PSDrive –Name “X” –PSProvider FileSystem –Root $sharefolder | out-null
Write-Host ======================================================
Write-Host = Searching for the destination folder. Please wait...
Write-Host ======================================================
$test1=Test-Path "X:\Program Files (x86)\BMC Software"
$test2=Test-Path "X:\Program Files\BMC Software"
if ($test1) {
    $file=Get-ChildItem "X:\Program Files (x86)\BMC Software" -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "CONTROL-SA"}
    } elseif ($test2){
    $file=Get-ChildItem "X:\Program Files\BMC Software" -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "CONTROL-SA"}
}
$fullname=$file.FullName+"\Services Manager\"+$Instance
$fullname
Write-Host ======================================================
Write-Host = Stopping $target
Write-Host ======================================================
Get-Service -Name $target -ComputerName $Server | Set-Service -Status Stopped
$src=$fullname+"\log\*"
$des=$fullname+"\log_bkp_"+$timestamp+"\"
Write-Host ======================================================
Write-Host = Perform LOG folder backup for troubleshooting
Write-Host ======================================================
New-Item $des -type directory | out-null
Move-item -path $src -destination $des -force -verbose
#Remove-item $src -force -recurse -verbose
Get-Service -Name $target -ComputerName $Server | Set-Service -Status Running
Remove-PSDrive -Name "X" | out-null
}
elseif ($result -eq 1) {
    BREAK    
}

Write-Host ======================================================
Write-Host = Process Completed
Write-Host = You may now close this window
Write-Host ======================================================
Start-Sleep -s 120

屏幕截图显示了结果..但是声誉低下使我无法发布它... :-(

我输入了我的意见,应该是控件而不是控件,但这就是问题所在。 $Form1.Controls.Add($label)添加新行:

$Form1.Controls.Add($Button)

看看当时它是否无法正常工作。 当我测试它对我有用。

$ Form1.Controls.Add($ Button)的错误位置阻止了我的代码显示控制按钮...

谢谢你的提示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM