我编写了一个脚本来导出SAP中的文档,并在Excel中运行这些宏。一切都在我的电脑上完美运作。 不幸的是,要求是在几台计算机上运行此脚本。问题是我无法在另一台计算机上阅读PowerShell中的XML。
为了解释我的PowerShell脚本,首先我将文档导出SAP,这非常有效。当我尝试读取XML文件以获取文档的特定路径时,出现以下错误。
无法找到类型[xml]:确保已加载包含此类型的程序集。
然后我试图找出我必须添加哪个程序集来读取XML。所以我第一次尝试:
Add-Type -AssemblyName System.Runtime.Serialization
没有工作。然后我尝试用System.XML.XmlDocument
创建一个对象。
$xml = New-Object System.Xml.XmlDocument
$xml.Load("C:\Users\mtn\AppData\Roaming\KPIReport\DIS.xml")
[xml] $xdoc = Get-Content "C:\Users\mtn\AppData\Roaming\KPIReport\DIS.xml"
$xdoc.SelectNodes("//DIS_PDP")
我遇到以下错误:
术语' New-Object System.Xml.XmlDocument'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
我检查了PowerShell的执行策略以运行脚本,但它仍然无效。
不知道问题出在哪里。为了解您的PowerShell代码:
$profile = $env:userprofile
$vbsPDPPath = "$env:userprofile\AppData\Roaming\KPIReport"
$vbsPDPName = "SAP-ExportPDP.vbs"
$processNamePDP = $vbsPDPPath + "\" + $vbsPDPName
Start-Process $processNamePDP -WindowStyle Hidden -Wait
$vbsFOLDERPath = "$env:userprofile\AppData\Roaming\KPIReport"
$vbsFOLDERName = "SAP-ExportFOLDER.vbs"
$processNameFOLDER = $vbsFOLDERPath + "\" + $vbsFOLDERName
Start-Process $processNameFOLDER -WindowStyle Hidden -Wait
# read the registry entry to know which file to use
$KeyPath = 'Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ Explorer\User Shell Folders'
$ValueName = 'Report'
$PathToXML = (Get-ItemProperty -Path $KeyPath -Name $ValueName).$ValueName
# parse XML
[xml] $report = Get-Content $PathToXML
# get the number of files, that have to be located in the specific folder
# to run the macro accurate
$counter = $report.Reports.Report.DIS_PDP.Count + $report.Reports.Report.DIS_FOLDER.Count
# if condition to be sure that everything is saved in the folder
if ((Get-ChildItem $profile\AppData\Roaming\KPIReport\Test | Measure-Object).Count -eq $counter) {
# run the personal macro for every PATH_PDP in the XML
foreach ($FilePath in $report.Reports.Report.PATH_PDP) {
$i = "$($profile)$FilePath"
$excel = New-Object -comobject Excel.Application
$excel.WindowState = 2
$excel.Visible = $false
$wbPersonalXLSB = $excel.Workbooks.Open($profile + $PersonalXLSB)
$workbook = $excel.Workbooks.Open($i)
$worksheet = $workbook.Worksheets.Item(1)
$excel.Run($RunMacro)
$wbPersonalXLSB.Close()
$workbook.Save()
$workbook.Close()
$excel.Quit()
}
# delete the temporary folder with the documents
$deleteFiles = Get-Item $profile\AppData\Roaming\KPIReport\Test
$deleteFiles | Remove-Item -ErrorAction SilentlyContinue -Force -Recurse
} else {
MsgBox("foo")
}
错误通常指的是这一行:
[xml] $report = Get-Content $PathToXML
但是在我的电脑上它没有错误。