【问题标题】:Powershell could not find part of the pathPowershell 找不到部分路径
【发布时间】:2018-09-13 17:24:49
【问题描述】:

在这里待了几个小时。我正在尝试从 Windows 机器中获取软件列表,并将该列表导出为 CSV,使用 $env:userprofile 的位置和文件“Software Inventories\$Username.csv”的路径

我收到以下错误

Export-Csv : Could not find a part of the path 'C:\Users\mylogin\Software Inventories\@{UserName=domain\mylogin}.csv'.
At C:\Users\mylogin\Dropbox\Powershell\GetSoftwareList.ps1:24 char:191
+ ... e, Vendor, Version | Sort-Object DisplayName | Export-Csv -Path $Path
+                                                    ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], DirectoryNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

代码如下:

$ComputerName = Read-Host 'Enter IP or computer name'
$WindowsVersion = Get-WmiObject -ComputerName $ComputerName -Credential 'domain\myadmin' -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption


If ($WindowsVersion -match 'Microsoft Windows 10 Enterprise'){

    $Username = Get-CimInstance -ComputerName $ComputerName -ClassName Win32_ComputerSystem | Select-Object UserName
    $Path = Join-Path $env:userprofile "Software Inventories\$Username.csv"
    Get-CimInstance -ComputerName $ComputerName -Credential "domain\myadmin" -ClassName Win32_InstalledWin32Program | Select Name, Vendor, Version | Sort-Object Name | Export-Csv -Path $Path
    Write-Host "Windows version is $WindowsVersion"

}Elseif ($WindowsVersion -match 'Microsoft Windows 7 Enterprise') {

    $Username = Get-WmiObject -ComputerName $ComputerName -Class Win32_ComputerSystem | Select-Object UserName
    $Path = Join-Path $env:userprofile "Software Inventories\$Username.csv"
    Get-WmiObject -ComputerName $ComputerName -Credential 'domain\myadmin' -Class Win32Reg_AddRemovePrograms | Select DisplayName, Vendor, Version | Sort-Object DisplayName | Export-Csv -Path $Path
    Write-Host "Windows version is $WindowsVersion"

我没有正确使用 Join-Path 吗?

顺便说一句,我怎样才能从 $Username 中只获取用户名作为字符串而忽略其余部分?

【问题讨论】:

  • 要拆分用户名,请使用此行 $Username = (Get-CimInstance -ComputerName $ComputerName -ClassName Win32_ComputerSystem | Select-Object UserName).UserName.Split("\")[1]

标签: powershell path


【解决方案1】:
$Username = (Get-CimInstance -ComputerName $ComputerName -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty UserName).Split('\')[1]
$Username = (Get-WmiObject -ComputerName $ComputerName -Class Win32_ComputerSystem | Select-Object -ExpandProperty UserName).Split('\')[1]

【讨论】:

  • 谢谢!那行得通。所以据我了解。 \ 分隔符省略了它之前的所有内容,只保存它之后的内容
  • 该行中的“[1]”有什么功能?
  • UserName 属性将返回格式为“[Domain]\[UserName]”的字符串。使用拆分.Split('\'),我们可以完全按照它所说的去做,并使用'\'(反斜杠)作为分隔符将字符串分成两部分。输出是两个字符串的零索引数组。第一个元素是数组"domain\user".Split('\')[0]中索引为0的域,第二个元素是数组中下一个索引号为1的用户名"domain\user".Split('\')[1]
【解决方案2】:

变量 $UserName 是一个哈希表,其属性名为 UserName,值为 domain\mylogin。要仅选择属性的值,请使用 Select-Object 中的 ExpandProperty 参数。

$Username = Get-CimInstance -ComputerName $ComputerName -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty UserName

【讨论】:

  • 非常感谢 $Username 变量的解释 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 2018-07-03
  • 2017-05-03
  • 1970-01-01
相关资源
最近更新 更多