PowerShell使用GUI创建DHCP范围远程服务器



我想在远程服务器上创建DHCP范围。远程服务器未连接。我想使用Invoke-Command ScriptBlock中的声明变量。

代码:

$DNS1 = "1XX.1XX.101.20","1XX.1XX.101.21"
$DNS2 = "194.239.134.83","193.162.153.164"
$DHCPServer = "XXXXXX"
$DHCPServer1 = "1XX.1X.10X.25"
# Input Box
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$scopename = [Microsoft.VisualBasic.Interaction]::InputBox("Indtast Scope navn", "Scope navn", "")
$3rdOctet = [Microsoft.VisualBasic.Interaction]::InputBox("Indtast 3 Oktet", "3 Oktet", "")
Write-Host
Write-Host ----------Preconfigured Settings----------- -foregroundcolor "yellow"
Write-Host
Write-Host Server: {}{}{}{}{}{}{}{} $dhcpserver -foregroundcolor "yellow"
Write-Host Scope Name: {}{}{}{} $scopename -foregroundcolor "yellow"
#Write-Host Scope ID: {}{}{}{}{}{} $scopeID -foregroundcolor "yellow"
#Write-Host IP Range: {}{}{}{}{}{} $startrange - $endrange -foregroundcolor "yellow"
#Write-Host Subnetmask: {}{}{}{} $subnetmask -foregroundcolor "yellow"
#Write-Host Router: {}{}{}{}{}{}{}{} $router -foregroundcolor "yellow"
Write-Host
Write-Host ---------/Preconfigured Settings----------- -foregroundcolor "yellow"
Write-Host
Write-Host
Write-Host
Write-Host Type in y to continue or any key to cancel...
Write-Host
$input = [Microsoft.VisualBasic.Interaction]::InputBox("Type in y to continue `n or any key to cancel...", "Create Scope", "")
if (($input) -eq "y" )
{   
    $MyCredentials=GET-CREDENTIAL –credential “XXXXXX”
    $sessionOption = New-PSSessionOption –SkipCACheck –SkipCNCheck -SkipRevocationCheck 
    $Session = New-PSSession -ComputerName 1XX.1X.10X.25 -Credential $MyCredentials -Authentication default -SessionOption $sessionOption
    Invoke-Command -Session $Session -ScriptBlock  {
        Add-DhcpServerv4Scope -ComputerName $DHCPServer1 -EndRange 1XX.1X.$3rdOctet.254 -Name $ScopeName -StartRange 1XX.1X.$3rdOctet.100 -SubnetMask 255.255.255.0 -LeaseDuration 8.0:0:0 -State Active -Type Dhcp
        Set-DhcpServerv4OptionValue -ComputerName $DHCPServer1 -ScopeId 1XX.1X.$3rdOctet.0 -Router 1XX.1X.$3rdOctet.1
        Set-DhcpServerv4OptionValue -ComputerName $DHCPServer1 -ScopeId 1XX.1X.$3rdOctet.0 -DnsServer $DNS2
    }
}
else 
{
     exit
}

最好的问候索伦。

Invoke-Command -Session $Session -ScriptBlock  {
Add-DhcpServerv4Scope -ComputerName $using:DHCPServer1 -EndRange "1XX.1X.$using:3rdOctet.254" -Name $using:ScopeName -StartRange "1XX.1X.$using:3rdOctet.100" -SubnetMask 255.255.255.0 -LeaseDuration 8.0:0:0 -State Active -Type Dhcp
Set-DhcpServerv4OptionValue -ComputerName $using:DHCPServer1 -ScopeId "1XX.1X.$using:3rdOctet.0" -Router "1XX.1X.$using:3rdOctet.1"
Set-DhcpServerv4OptionValue -ComputerName $using:DHCPServer1 -ScopeId "1XX.1X.$using:3rdOctet.0" -DnsServer $using:DNS2
}

PowerShell的工作方式,您必须通过$using:关键字或使用-ArgumentList传递变量(但是您必须使用param()指令使用-ArgumentList)。
https://technet.microsoft.com/en-us/library/jj149005.aspx

相关内容

  • 没有找到相关文章

最新更新