2017-06-02 101 views
0

我有这个脚本,我试图运行,我希望将备份DNS区域。我正尝试使用export-csv powershell cmdlet将此信息导出到csv文件中。最后,我使用dnscmd.exe命令将区域信息导出到文本文件并将它们存储在定义的位置。在运行PowerShell脚本来备份DNS区域时遇到Get-WmiObject问题

# Get Name of the server with env variable 
$DNSSERVER=get-content env:computername 

#—Define folder where to store backup —–# 
$BkfFolder=”c:\windows\system32\dns\backup” 

#—Define file name where to store Dns Settings 
$StrFile=Join-Path $BkfFolder “input.csv” 

#—-Check if folder exists. if exists, delete contents–# 
if (-not(test-path $BkfFolder)) { 
new-item $BkfFolder -Type Directory | Out-Null 
} else { 

Remove-Item $BkfFolder”\*” -recurse 
} 

#—- GET DNS SETTINGS USING WMI OBJECT ——–# 
#– Line wrapped should be only one line –# 
$List = get-WmiObject -ComputerName $DNSSERVER 
-Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone 

#—-Export information into input.csv file —# 
#– Line wrapped should be only one line –# 
$list | Select Name,ZoneType,AllowUpdate,@{Name=”MasterServers”;Expression={$_.MasterServers}}, 
DsIntegrated | Export-csv $strFile -NoTypeInformation 

#— Call Dnscmd.exe to export dns zones 
$list | foreach { 
$path=”backup\”+$_.name 
$cmd=”dnscmd {0} /ZoneExport {1} {2}” -f $DNSSERVER,$_.Name,$path 
Invoke-Expression $cmd 
} 

# End of Script 
#——————————————————————————————-# 

当我运行该脚本,我得到以下信息:

enter image description here

我不完全相信这个消息是说。我尝试输入我的电脑名称,但这也不起作用。

任何帮助表示赞赏!

+0

在'-Namespace ...'之前删除换行符,以便所有参数在同一行上 –

+0

..或在$ DNSSERVER后添加空格和反引号... – thepip3r

回答

0

从2号线:

$DNSSERVER=get-content env:computername 

应该是:

$DNSSERVER = $Env:Computername 

的错误是在这一行:

$List = get-WmiObject -ComputerName $DNSSERVER -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone 

确保它是在同一行,而不是单独的行,它正在请求gwmi命令的类,但是因为它在另一行中,它没有采用它。因为这个类确实存在in here所以问题应该在这个特定的行中。

另一点,它只是寻找DNS类,只有在Windows服务器上安装了DNS功能或角色时才能工作。