mattsibley Posted May 7, 2017 Share Posted May 7, 2017 I am finding that I am having an error with some PowerShell code that I am working on. What I want to be able to do is to have a variable, say $uk, where the drive label name is 'backup' and then force the creation of folders using the declared variable as the drive letter eg $uk\$env:computername\$env:username . The code is pasted below: #Asssigns $uk Variable to current drive letter for the mounted volume where the label name = backup $uk = Get-WmiObject Win32_Volume | ? {$_.label -eq 'backup'} #Creates a directory on the C:\ Drive naming the folders [The Computer Name] and [User] and ignores if already exists. md $uk\$env:computername\$env:username -force | Out-Null #It then uses Get-WmiObject to get the OS information and exports this info to the created directory calling the file export.csv. Get-WmiObject -class win32_operatingsystem | Export-Csv $uk\$env:computername\$env:username\export.csv #Then echos Export Complete into console. echo "Export Complete" #Sleep for two seconds. Start-Sleep -s 2 #Kill Powershell process. #stop-process -Id $Pid However, when this runs I get this error: md : Illegal characters in path. At C:\Users\Matt\OneDrive\Documents\Matt's Documents\ExportOSInfo.ps1:8 char:1 + md $uk\$env:computername\$env:username -force | Out-Null + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (\\MATT-PC\root\...6ed}\\"\MATT-PC:String) [New-Item], Argu mentException + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.NewItemCommand md : Illegal characters in path. At C:\Users\Matt\OneDrive\Documents\Matt's Documents\ExportOSInfo.ps1:8 char:1 + md $uk\$env:computername\$env:username -force | Out-Null + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (\\MATT-PC\root\...6ed}\\"\MATT-PC:String) [New-Item], Argu mentException + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand md : Illegal characters in path. At C:\Users\Matt\OneDrive\Documents\Matt's Documents\ExportOSInfo.ps1:8 char:1 + md $uk\$env:computername\$env:username -force | Out-Null + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (\\MATT-PC\root\...\"\MATT-PC\Matt:String) [New-Item], Argu mentException + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand Export-Csv : Cannot perform operation because the wildcard path \\MATT-PC\root\cimv2:Win32_Volume.DeviceID= "\\\\?\\Volume{2277fba4-e118-11e3-8533-94de807ff6ed}\\"\MATT-PC\Matt\export.csv did not resolve to a file. At C:\Users\Matt\OneDrive\Documents\Matt's Documents\ExportOSInfo.ps1:10 char:46 + ... ingsystem | Export-Csv $uk\$env:computername\$env:username\export.csv ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (\\MATT-PC\root\...Matt\export.csv:String) [Export-Csv], FileNotF oundException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand Obviously there is an error in the file path where it is trying to get the volume. I am not too sure how i would fix this tried wrapping the variable name in ${} and with a :\ after the variable eg $uk:\, neither of these produce the desired result. Can anyone help? Matt Quote Link to comment Share on other sites More sharing options...
PoSHMagiC0de Posted May 7, 2017 Share Posted May 7, 2017 5 hours ago, mattsibley said: #Asssigns $uk Variable to current drive letter for the mounted volume where the label name = backup $uk = Get-WmiObject Win32_Volume | ? {$_.label -eq 'backup'} The line above is the issue. You are passing the whole volume object and then trying to use it. If you want to use it as a single variable, you have to separate out the driveletter part. $uk = (Get-WmiObject Win32_Volume | ? {$_.label -eq 'backup'}).DriveLetter Quote Link to comment Share on other sites More sharing options...
mattsibley Posted May 7, 2017 Author Share Posted May 7, 2017 @PoSHMagiC0de Thanks, mate! I was struggling for a bit there. I will make sure to mention you in the code's comments when it is done. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.