2016-06-11 115 views
0

我正在使用appcmd.exe運行此腳本以將IP列入白名單。在powershell中使用appcmd.exe白名單IP?

import-module WebAdministration 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$Form = New-Object System.Windows.Forms.Form  
$Form.Size = New-Object System.Drawing.Size(600,400) 
$type="Allow" 

function SaveConfig 
{ 

if($Dropdownbox.text -eq "allow"){ 
$allowed = "true" 
} 
else{ 
$allowed = "false" 
} 
$outputbox.text = "IP " + $ip.text + " is Whitelisted for the URL " + $url.text + " with subnet mask as " + $mask.text + ". " + "User wants to " + $Dropdownbox.text + " this." 

<# $url = "capitaliq/ciqdotnet/clientadmin/clientmgr.html" 
$ip = "192.168.1.1" #> 

$url.text 
Set-Location "C:\Windows\System32\inetsrv"; .\appcmd.exe set config "$url.text" -section:system.webServer/security/ipSecurity /+"[ipAddress='$ip.text',allowed='True',subnetMask='$mask.text']" /commit:apphost 
$ip.text 

$url.text = "" 
$ip.text = "" 
$dropdownbox.text = "" 
} 

function close{ 
$Form.close() 
} 

$url_label = New-Object System.Windows.Forms.Label 
$url_label.Location = New-Object System.Drawing.Size(40,20) 
$url_label.Size = New-Object System.Drawing.Size(280,20) 
$url_label.Text = "Please enter the URL" 
$Form.Controls.Add($url_label) 

$url = New-Object System.Windows.Forms.TextBox 
$url.Location = New-Object System.Drawing.Size(40,50) 
$url.Size = New-Object System.Drawing.Size(260,60) 
$Form.Controls.Add($url) 

$ip_label = New-Object System.Windows.Forms.Label 
$ip_label.Location = New-Object System.Drawing.Size(40,110) 
$ip_label.Size = New-Object System.Drawing.Size(280,20) 
$ip_label.Text = "Please enter the IP address" 
$Form.Controls.Add($ip_label) 

$ip = New-Object System.Windows.Forms.TextBox 
$ip.Location = New-Object System.Drawing.Size(40,140) 
$ip.Size = New-Object System.Drawing.Size(260,60) 
$Form.Controls.Add($ip) 

$DropDownBox = New-Object System.Windows.Forms.ComboBox 
$DropDownBox.Location = New-Object System.Drawing.Size(40,80) 
$DropDownBox.Size = New-Object System.Drawing.Size(180,20) 
$DropDownBox.DropDownHeight = 400 
$Form.Controls.Add($DropDownBox) 

[email protected]("Allow","Deny") 

foreach ($wks in $wksList) 
{ 
$DropDownBox.Items.Add($wks) 
} 

$mask_label = New-Object System.Windows.Forms.Label 
$mask_label.Location = New-Object System.Drawing.Size(40,170) 
$mask_label.Size = New-Object System.Drawing.Size(280,20) 
$mask_label.Text = "Please enter the Subnet Mask" 
$Form.Controls.Add($mask_label) 

$mask = New-Object System.Windows.Forms.TextBox 
$mask.Location = New-Object System.Drawing.Size(40,200) 
$mask.Size = New-Object System.Drawing.Size(260,60) 
$mask.Text="255.255.255.0" 
$Form.Controls.Add($mask) 


$Button = New-Object System.Windows.Forms.Button 
$Button.Location = New-Object System.Drawing.Size(40,230) 
$Button.Size = New-Object System.Drawing.Size(110,50) 
$Button.Text = "Save" 
$Button.Add_Click({SaveConfig}) 
$Form.Controls.Add($Button) 

$Button = New-Object System.Windows.Forms.Button 
$Button.Location = New-Object System.Drawing.Size(170,230) 
$Button.Size = New-Object System.Drawing.Size(110,50) 
$Button.Text = "Close" 
$Button.Add_Click({Close}) 
$Form.Controls.Add($Button) 

$outputBox = New-Object System.Windows.Forms.TextBox 
$outputBox.Location = New-Object System.Drawing.Size(350,50) 
$outputBox.Size = New-Object System.Drawing.Size(200,200) 
$outputBox.MultiLine = $True 
$outputBox.ReadOnly= $True 
$Form.Controls.Add($outputBox) 

$Form.Add_Shown({$Form.Activate()}) 
[void] $Form.ShowDialog() 

出於某種原因,是沒有得到執行這行代碼:

Set-Location "C:\Windows\System32\inetsrv"; .\appcmd.exe set config "$url.text" -section:system.webServer/security/ipSecurity /+" [ipAddress='$ip.text',allowed='True',subnetMask='$mask.text']" /commit:apphost 

連接的是這表明腳本只設置位置所需的,並沒有別的形象。 $ url.text和$ ip.text在這行之前和之後也沒有得到執行。

enter image description here

回答

1

它肯定執行appcmd.exe,你只是沒有看到Click事件的輸出,因爲它是在自己的範圍內運行。

您的appcmd.exe最有可能失敗,因爲您嘗試在雙引號字符串內擴展$ip.text。分析器將整個$ip對象轉換爲字符串,並串連文本字符串「的.text」,從而產生以下appcmd.exe論點:

/+" [ipAddress='System.Windows.Forms.TextBox, Text: .text',allowed='True',subnetMask='System.Windows.Forms.TextBox, Text: .text']" 

括在一個子表達式$ip.Text$mask.Text$())代替:

.\appcmd.exe set config "$($url.Text)" -section:system.webServer/security/ipSecurity /+" [ipAddress='$($ip.Text)',allowed='True',subnetMask='$($mask.Text)']" /commit:apphost 
+0

爲什麼不打印$ url.text,我正試圖在set location命令之前打印? –

+0

'Write-Host $ url.text' –