从powershell导出到txt文件

时间:2015-01-19 15:45:41

标签: powershell-v3.0

我试图通过powershell在dhcp上设置范围并将信息导出到txt文件。我的代码做了它需要做的事情,但当我用|导出到txtfile时out文件只导出保留的ip而不是ip-range和exclude-range我需要改变什么

#scherm leegmaken
Clear-Host

#import module dhcp server
Import-Module DHCPServer 

#DHCP service oppstarten
Set-service dhcpserver –startuptype automatic 
Start-service dhcpserver
#Een scope aanmaken
netsh dhcp server 192.168.126.140 add scope  192.168.126.0 255.255.255.0 "Scope1"
#de iprange van de scope meegeven
netsh dhcp server 192.168.126.140 scope 192.168.126.0 add iprange 192.168.126.160 192.168.126.199
#de excluderange meegeven
netsh dhcp server 192.168.126.140 scope 192.168.126.0 add excluderange 192.168.126.180 192.168.126.189
#Een ip reserveren
netsh dhcp server 192.168.126.140 scope 192.168.126.0 add reservedip 192.168.126.182 f8db7f4fa490 "reservationEx1"



#de iprange, ecluderange en het gereserveerde ip weerrgeven en wegschrijven naar txt file
netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show iprange +

netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show excluderange +

netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show reservedip | Out-File "C:\TEMP\Ballali.DHCPConfigInfo.11792.txt"

2 个答案:

答案 0 :(得分:0)

没关系,我已经找到了解决方案。 我需要使用参数-Append来覆盖txt文件。 并将外部文件cmd而不是+

答案 1 :(得分:0)

问题在于操作顺序。 你的代码:

netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show iprange +
netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show excluderange +
netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show reservedip | Out-File "C:\TEMP\Ballali.DHCPConfigInfo.11792.txt"

相当于此!

netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show iprange +
netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show excluderange +
(netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show reservedip | Out-File "C:\TEMP\Ballali.DHCPConfigInfo.11792.txt")

你在做什么

  1. 保留的IP地址列表写入指定的文件。
  2. 第1,第2个ip范围列表和Out-File返回的结果放在一起。
  3. 因为我更喜欢明确声明,所以我会喜欢以下

    $iprange = netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show iprange
    $exclude_range = netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show excluderange
    $reserved_range = netsh DHCP server \\W2K12PWSHLL01 scope '192.168.126.0' show reservedip
    ($iprange + $exclude_range + $reserved_range) | Out-File "C:\TEMP\Ballali.DHCPConfigInfo.11792.txt")