在这里呆了几个小时。我正在尝试从Windows计算机上获取软件列表,并使用$ env:userprofile位置和文件“ Software Inventories \ $ Username.csv”的路径将该列表作为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中仅获取用户名作为字符串,而忽略其余部分?
答案 0 :(得分:2)
变量$ UserName是一个哈希表,具有一个名为UserName的属性和一个domain \ mylogin的值。要仅选择属性的值,请使用Select-Object中的ExpandProperty参数。
$Username = Get-CimInstance -ComputerName $ComputerName -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty UserName
答案 1 :(得分:2)
$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]