Powershell要安装。\n文件错误,但在给定完整位置C:\Users。。。。不';t错误

Powershell要安装。\n文件错误,但在给定完整位置C:\Users。。。。不';t错误,powershell,installation,windows-installer,Powershell,Installation,Windows Installer,我在下面有一个脚本,在尝试访问文件时出错,但是如果我将.msi文件在-argumentlist中的位置更改为完整地址,则会成功,但我不能让它像这样运行,因为当我提交它以打包用于SCCM部署时,地址会更改 Function Get-OSCComputerOU { $ComputerName = $env:computername $Filter = "(&(objectCategory=Computer)(Name=$ComputerName))" $Di

我在下面有一个脚本,在尝试访问文件时出错,但是如果我将.msi文件在-argumentlist中的位置更改为完整地址,则会成功,但我不能让它像这样运行,因为当我提交它以打包用于SCCM部署时,地址会更改

    Function Get-OSCComputerOU
{
    $ComputerName = $env:computername
    $Filter = "(&(objectCategory=Computer)(Name=$ComputerName))"

    $DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher
    $DirectorySearcher.Filter = $Filter
    $SearcherPath = $DirectorySearcher.FindOne()
    $DistinguishedName = $SearcherPath.GetDirectoryEntry().DistinguishedName

    $OUName = ($DistinguishedName.Split(","))[1]
    $OUMainName = $OUName.SubString($OUName.IndexOf("=")+1)

    $OUMainName
}
$strOU = Get-OSCComputerOU
$strTrueOU=$strOU.split('_')[1]
$strCSV=Import-Csv \\SERVER\SHARE\FOLDER\CSV.csv
$strRoomChannel=$strCSV | where {$_.Room -eq $strTrueOU} | % channel
IF ($strRoomChannel){
$strRoomFoundArg="/i .\Installers\MSI.msi CHANNEL=$strRoomChannel"
Start-Process msiexec -ArgumentList $strRoomFoundArg -wait
} ELSE {
msg * "Channel is missing, and can not install correctly, please call tech support on Ext: to have this rectified, it's a quick fix."
}
当我使用下面这样的完整地址时,它安装得很好……怎么回事

    Function Get-OSCComputerOU
{
    $ComputerName = $env:computername
    $Filter = "(&(objectCategory=Computer)(Name=$ComputerName))"

    $DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher
    $DirectorySearcher.Filter = $Filter
    $SearcherPath = $DirectorySearcher.FindOne()
    $DistinguishedName = $SearcherPath.GetDirectoryEntry().DistinguishedName

    $OUName = ($DistinguishedName.Split(","))[1]
    $OUMainName = $OUName.SubString($OUName.IndexOf("=")+1)

    $OUMainName
}
$strOU = Get-OSCComputerOU
$strTrueOU=$strOU.split('_')[1]
$strCSV=Import-Csv \\SERVER\SHARE\FOLDER\CSV.csv
$strRoomChannel=$strCSV | where {$_.Room -eq $strTrueOU} | % channel
IF ($strRoomChannel){
$strRoomFoundArg="/i C:\Users\USERNAME\Desktop\Installers\MSI.msi CHANNEL=$strRoomChannel"
Start-Process msiexec -ArgumentList $strRoomFoundArg -wait
} ELSE {
msg * "Channel is missing, and can not install correctly, please call tech support on Ext: to have this rectified, it's a quick fix."
}
我得到这个错误:


两者之间的区别在于,“.”将由您正在调用的进程msiexec解决,它与大多数进程一样,将使用进程的CurrentDirectory for“.”,它与PowerShell中的当前位置不同。如果比较PowerShell中的
Get Location
[Environment]::CurrentDirectory]
,可以看到差异。如果启动powershell并使用
Set Location
(又称cd)更改目录,则它们将不同

解决方案是先解析PowerShell中的路径,然后再将其发送到msiexec:

$path = Convert-Path .\Installers\MSI.msi
$strRoomFoundArg = "/i `"$path`" CHANNEL=$strRoomChannel"
Start-Process msiexec -ArgumentList $strRoomFoundArg -wait

结果表明脚本对MSI文件前面的。\n不满意

如果我保留。\n,我会得到错误

如果我删除了。\n并且只使用了MSI.MSI,那么它工作得很好

我没有提到我已将active directory更改为我的桌面以执行脚本,我的道歉@mike z

不过,非常感谢您的意见