2016-06-11 64 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' –