在PowerShell中使用appcmd.exe将IP列入白名单?

时间:2016-06-11 14:23:46

标签: 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) 

$wksList=@("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 个答案:

答案 0 :(得分:1)

它肯定会执行<body> <div id="page-body"> <div id="overlay"> </div> <div id="content"> ...... </div> </div> </body> ,你只是看不到Click事件的输出,因为它在自己的范围内运行。

您的appcmd.exe很可能会失败,因为您尝试在双引号字符串中展开appcmd.exe。解析器将整个$ip.text对象转换为字符串并连接文字字符串“.text”,从而产生以下$ip参数:

appcmd.exe

/+" [ipAddress='System.Windows.Forms.TextBox, Text: .text',allowed='True',subnetMask='System.Windows.Forms.TextBox, Text: .text']" $ip.Text括在子表达式($mask.Text)中代替:

$()
相关问题