由于目录名称中的 space,PowerShell 执行失败
PowerShell execution fails due to space in directory name
我收到了一个 PowerShell 脚本,该脚本用于安装位于同一目录中的可执行文件。这被打包到 SCCM(System Center Configuration Manager)中进行部署。但是,如果在包部署期间推送包的临时目录在文件路径中的任何位置有 space。
,则部署失败
例如:
- C:\temp\deployment -- 这将通过,软件将安装。
- C:\temp\deployment 1\ -- 这将失败 由于 space 在部署和1
- C:\temporary directory\deployment\ -- 这将失败 由于space和目录。
这是我怀疑正在执行安装的部分代码:
if ($softwarename -eq "Not Installed") {
Write-Output "Installing softwarename..."
# Build a massive softwarename installation line
$tempFile = [System.IO.Path]::GetTempFileName() + ".cmd"
$installLine = "$scriptPath\softwarename.exe /s /v`"/qn INSTALLDIR=\`"C:\Program Files\directoryname\softwarename\`" AUTHTOKEN=REDACTED FULLCONSOLEADDRESS=$dest`:8413 HOSTNAME=$Env:COMPUTERNAME LOG_SOURCE_AUTO_CREATION_ENABLED=True LOG_SOURCE_AUTO_CREATION_PARAMETERS=`"`"Component1.AgentDevice=DeviceWindowsLog&Component1.Action=create&Component1.LogSourceName=$env:COMPUTERNAME&Component1.LogSourceIdentifier=$env:COMPUTERNAME&Component1.Log.Security=true&Component1.Filter.Security.Enabled=true&Component1.Filter.Security.Param=5156&Component1.Filter.Security.Type=Blacklist&Component1.Log.System=true&Component1.Log.Application=true&Component1.Log.DNS+Server=false&Component1.Log.File+Replication+Service=false&Component1.Log.Directory+Service=false&Component1.Destination.Name=$dest&Component1.RemoteMachinePollInterval=300&Component1.EventRateTuningProfile=High+Event+Rate+Server&Component1.MinLogsToProcessPerPass=1250&Component1.MaxLogsToProcessPerPass=1875`"`"`""
$installLine | Out-File -Encoding ascii -filepath $tempFile
Write-Output $tempFile
cmd.exe /c $tempFile
} else {
if ($psversiontable.psversion.major -gt 2) {
$props=ConvertFrom-StringData (Get-Content "c:\Program Files\directoryname\softwarename\config\install_config.txt" -Raw)
if ($props.ConfigurationServer -eq $dest) {
Write-Output "Configuration server is correct - no action"
} else {
Stop-Service (Get-Service -ErrorAction SilentlyContinue softwarename)
$props.ConfigurationServer=$dest
$props.StatusServer=$dest
$props.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Out-File -Encoding ascii -filepath "c:\Program Files\directoryname\softwarename\config\install_config.txt"
del "c:\Program Files\directoryname\softwarename\config\ConfigurationServer.PEM"
Start-Service (Get-Service -ErrorAction SilentlyContinue softwarename)
Write-Output "Configuration server reset to $dest"
}
} else {
Write-Output "Powershell version does not support the reset functionality."
}
我怀疑以下两行是问题的原因:
$tempFile = [System.IO.Path]::GetTempFileName() + ".cmd"
$installLine = "$scriptPath\softwarename.exe /s /v`"/qn
我怀疑因为执行需要准确的路径,目录路径中的 "space" 会导致 "path not found"(有趣的是在我的 Windows 10 机器更改目录 [cd] 命令上无需将带有 spaces 的文件路径放在引号中即可工作 - "" -- 这让我相信我完全错了,但在这一点上我没有其他要看的东西)。
任何人都可以协助添加参数以确保为要执行的 .cmd 文件生成的补丁没有 space 吗?
我试图通过添加批处理文件来修补此问题,该批处理文件在执行前将包复制到静态目录。但是,这无法通过 SCCM 进行部署。
为了从路径包含空格的批处理文件中调用程序,该路径必须包含在 "..."
:
中
# PowerShell string that constructs the command line to write to the batch file.
$installLine = "`"$scriptPath\softwarename.exe`" ..."
`"
是您在 PowerShell 的 "..."
字符串中 嵌入 文字 "
的方式。
相比之下,从包含空格的路径的批处理文件调用 cd
也可以 而无需 包含 "..."
,但是:
- 这只有在没有歧义的情况下才有可能,因为
cd
只需要 一个 操作数(目标目录)
- 由于所有其他命令必须处理带空格的参数的方式不一致,支持这从来都不是一个好主意。
我收到了一个 PowerShell 脚本,该脚本用于安装位于同一目录中的可执行文件。这被打包到 SCCM(System Center Configuration Manager)中进行部署。但是,如果在包部署期间推送包的临时目录在文件路径中的任何位置有 space。
,则部署失败例如:
- C:\temp\deployment -- 这将通过,软件将安装。
- C:\temp\deployment 1\ -- 这将失败 由于 space 在部署和1
- C:\temporary directory\deployment\ -- 这将失败 由于space和目录。
这是我怀疑正在执行安装的部分代码:
if ($softwarename -eq "Not Installed") {
Write-Output "Installing softwarename..."
# Build a massive softwarename installation line
$tempFile = [System.IO.Path]::GetTempFileName() + ".cmd"
$installLine = "$scriptPath\softwarename.exe /s /v`"/qn INSTALLDIR=\`"C:\Program Files\directoryname\softwarename\`" AUTHTOKEN=REDACTED FULLCONSOLEADDRESS=$dest`:8413 HOSTNAME=$Env:COMPUTERNAME LOG_SOURCE_AUTO_CREATION_ENABLED=True LOG_SOURCE_AUTO_CREATION_PARAMETERS=`"`"Component1.AgentDevice=DeviceWindowsLog&Component1.Action=create&Component1.LogSourceName=$env:COMPUTERNAME&Component1.LogSourceIdentifier=$env:COMPUTERNAME&Component1.Log.Security=true&Component1.Filter.Security.Enabled=true&Component1.Filter.Security.Param=5156&Component1.Filter.Security.Type=Blacklist&Component1.Log.System=true&Component1.Log.Application=true&Component1.Log.DNS+Server=false&Component1.Log.File+Replication+Service=false&Component1.Log.Directory+Service=false&Component1.Destination.Name=$dest&Component1.RemoteMachinePollInterval=300&Component1.EventRateTuningProfile=High+Event+Rate+Server&Component1.MinLogsToProcessPerPass=1250&Component1.MaxLogsToProcessPerPass=1875`"`"`""
$installLine | Out-File -Encoding ascii -filepath $tempFile
Write-Output $tempFile
cmd.exe /c $tempFile
} else {
if ($psversiontable.psversion.major -gt 2) {
$props=ConvertFrom-StringData (Get-Content "c:\Program Files\directoryname\softwarename\config\install_config.txt" -Raw)
if ($props.ConfigurationServer -eq $dest) {
Write-Output "Configuration server is correct - no action"
} else {
Stop-Service (Get-Service -ErrorAction SilentlyContinue softwarename)
$props.ConfigurationServer=$dest
$props.StatusServer=$dest
$props.GetEnumerator() | % { "$($_.Name)=$($_.Value)" } | Out-File -Encoding ascii -filepath "c:\Program Files\directoryname\softwarename\config\install_config.txt"
del "c:\Program Files\directoryname\softwarename\config\ConfigurationServer.PEM"
Start-Service (Get-Service -ErrorAction SilentlyContinue softwarename)
Write-Output "Configuration server reset to $dest"
}
} else {
Write-Output "Powershell version does not support the reset functionality."
}
我怀疑以下两行是问题的原因:
$tempFile = [System.IO.Path]::GetTempFileName() + ".cmd"
$installLine = "$scriptPath\softwarename.exe /s /v`"/qn
我怀疑因为执行需要准确的路径,目录路径中的 "space" 会导致 "path not found"(有趣的是在我的 Windows 10 机器更改目录 [cd] 命令上无需将带有 spaces 的文件路径放在引号中即可工作 - "" -- 这让我相信我完全错了,但在这一点上我没有其他要看的东西)。
任何人都可以协助添加参数以确保为要执行的 .cmd 文件生成的补丁没有 space 吗?
我试图通过添加批处理文件来修补此问题,该批处理文件在执行前将包复制到静态目录。但是,这无法通过 SCCM 进行部署。
为了从路径包含空格的批处理文件中调用程序,该路径必须包含在 "..."
:
# PowerShell string that constructs the command line to write to the batch file.
$installLine = "`"$scriptPath\softwarename.exe`" ..."
`"
是您在 PowerShell 的 "..."
字符串中 嵌入 文字 "
的方式。
相比之下,从包含空格的路径的批处理文件调用 cd
也可以 而无需 包含 "..."
,但是:
- 这只有在没有歧义的情况下才有可能,因为
cd
只需要 一个 操作数(目标目录) - 由于所有其他命令必须处理带空格的参数的方式不一致,支持这从来都不是一个好主意。