Reading values from offline registry file - powershell

I am trying to read an offline registry file:
$product_name = Get-ItemProperty -Path "C:\temp\RegistryHives\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -name ProductName | Select-Object -ExpandProperty ProductName
Getting the below error. What would be the right way to access the offline registry file keys and values?
Get-ItemProperty : Cannot find path 'C:\temp\RegistryHives\SOFTWARE\Microsoft\Windows NT\CurrentVersion' because it
does not exist.
At line:1 char:17
+ ... duct_name = Get-ItemProperty -Path "C:\temp\RegistryHives\SOFTWARE\Mi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\Registr...\CurrentVersion:String) [Get-ItemProperty], ItemNotFo
undException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand

You need to load the reg hive before accessing it. reg.exe can load hives with the load command`
Reg.exe load 'HKLM\TempHive' C:\temp\RegistryHives\SOFTWARE
$product_name = Get-ItemProperty -Path "HKLM:\TempHive\Microsoft\Windows NT\CurrentVersion" -name ProductName | Select-Object -ExpandProperty ProductName
Reg.exe unload 'HKLM\TempHive'

Related

Why does powershell throw a path does not exist error?

Why does powershell throw an error when trying to create or modify a registry key from the docs I used
Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmployees" -Value 823
The error I get
Set-ItemProperty : Cannot find path 'HKLM:\Software\ContosoCompany' because it does not exist.
At line:1 char:1
+ Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKLM:\Software\ContosoCompany:String) [Set-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand
I am running the powershell as an admin and I tried to modify existing keys and also tried to create new ones but I still get this error
HKLM is the name of a 'drive'. Move there first
Set-Location HKLM:
Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmployees" -Value 823

Exporting objectid

I have a list of device names exported to an csv file.
I need to import this csv to then query the data and export the objectid of these computers from azure.
I am using the below and the error I am receiving is beneath
Can anyone point me in the right direction?
$test3 = import-csv -path "C:\temp\test3.xml"
Get-MsolDevice -Name $test3.Name | Select-Object -Property ObjectID | Export-CSV -Path "C:\temp\test9.xml"**
Get-MsolDevice : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported.
At line:1 char:22
+ Get-MsolDevice -Name $test3.Name | Select-Object -Property ObjectID | ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-MsolDevice], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Online.Administration.Automation.GetDevice
$test3.Name is going to return a collection of strings if there is more than one row in the CSV. You'll need to iterate over each like so:
$objectIds = $test3.Name | Foreach-Object {
Get-MsolDevice -Name $_ |
Select-Object -Property ObjectID
}
$objectIds | Export-Csv -Path "C:\temp\test9.xml" -NoTypeInformation

Powershell latest MSBuild path

After trying a number of approaches, including what seemed an excellent suggestion at http://www.bdevuyst.com/powershell-path-msbuild-exe/, which gave me an error, I tried to break it down. Though I get the latest MSBuild registry key (14.0), I still get this error when I try to extract the path to MSBuild:
Get-ItemProperty : Cannot find path 'C:\USERS\user\Desktop\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSBuild\ToolsVersions\14.0' because it does not exist.
At C:\USERS\mtroi\Desktop\VSS_POC1_Setup.ps1:529 char:5
+ Get-ItemProperty -Path $MsBuildVersion -Name MSBuildToolsPath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\USERS\mtroi\...lsVersions\14.0:String) [Get-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
# 32bit or 64bit local OS?
if($ENV:PROCESSOR_ARCHITECTURE -eq "x86")
{$HKLMpath = "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\"}
elseif($ENV:PROCESSOR_ARCHITECTURE -eq "AMD64")
{$HKLMpath = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSBuild\ToolsVersions\"}
else
{Write-Host "Local processor architecture not supported. Exiting installation..."; EXIT}
# Which path (version) of local MSBuild?
$_decSep = [System.Threading.Thread]::CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator;
$MsBuild1 = #(Get-ChildItem -Path $HKLMpath | Where { $_.Name -match '\\\d+.\d+$' })
ForEach ($build in $MsBuild1)
{
$Expression=$Expression+","+[System.Convert]::ToDecimal($build.Name.Substring($build.Name.LastIndexOf("\") + 1))
}
$MsBuildVersion = Get-ChildItem -Path $HKLMpath | Where { $_.Name -match '\\\d+.\d+$' } |
Sort-Object -Property #{Expression=$Expression} -Descending |
Select-Object -First 1 #| Get-ItemProperty -Name MSBuildToolsPath
Get-ItemProperty -Path $MsBuildVersion -Name MSBuildToolsPath

Access to path denied while creating new file on first iteration of loop

I have a strange problem when writing data to a new file. I have a list of files in a directory that contains data I'm parsing and returning data with the Create-VMwareconf() function. This returns the data in a hashtable that I've assigned to $t. I've pulled the desired folder and filename from the $t function however each time I begin the loop I get the following error for the initial folder creation, the second and third work fine. Interestingly enough the data that should be in the first file is present in the second folder.
If I run the script again it generates all three objects however the sequence of the of the data in the file matching the file name is incorrect.
Any help would be appreciated in how to stop the following error;
$e = (Get-Childitem ".\a\*\*.ini")
Set-Location "C:\WindowsRoot\vmwareconfigfiles\"
ForEach($d in $e){
$vmwaredirectory = New-item -type directory -path .\ -name $dd -Force
$vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force
$t = Create-VMwareconf($d)
$dd = $t.Value["0"]
#Write contents to new file
$t | Out-File $vmwarefile
}
Error received on initial run;
New-Item : Access to the path 'C:\WindowsRoot\vmwareconfigfiles' is denied.
At C:\WindowsRoot\parsedisrec.ps1:93 char:15
+ $vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\WindowsRoot\vmwareconfigfiles:String) [New- Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At C:\WindowsRoot\parsedisrec.ps1:97 char:15
+ $t | Out-File $vmwarefile
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand
New-Item -Type file fails during the first iteration, because $dd isn't yet initialized then, so you're trying to create a file with the same name as the current directory. You'd get the the same result if you used $null (or even .) as the name:
PS C:\> New-Item -Type file -Path 'C:\some\where' -Name $null -Force
New-Item : Access to the path 'C:\some\where' is denied.
At line:1 char:1
+ New-Item -Type file -Path 'C:\some\where' -Name $null -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\some\where:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
PS C:\> New-Item -Type file -Path 'C:\some\where' -Name '.' -Force
New-Item : Access to the path 'C:\some\where' is denied.
At line:1 char:1
+ New-Item -Type file -Path 'C:\some\where' -name '.' -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\some\where\.:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
Change this:
ForEach($d in $e){
$vmwaredirectory = New-item -type directory -path .\ -name $dd -Force
$vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force
$t = Create-VMwareconf($d)
$dd = $t.Value["0"]
into this:
ForEach($d in $e){
$t = Create-VMwareconf($d)
$dd = $t.Value["0"]
$vmwaredirectory = New-item -type directory -path .\ -name $dd -Force
$vmwarefile = New-Item -type file -path $vmwaredirectory -name $dd -Force

Get-Childitem trying to get the registry entry from current working directory

I am trying to get registry key values but i am facing issues.
Following is the command i tried to execute.
Get-ChildItem "HKEY_LOCAL_MACHINE\SFTWARE\Wow6432Node\Mycompany\MyProj\Model" | ForEach-Object {Get-ItemProperty $_.pspath}
But in registry key path appended with current working directory and produce unexpected issue like below.
PS C:\Users\Administrator\Desktop\MyDoc\Core\NodeRenameMaster> Get-ChildItem "HKEY_LOCAL_MACHINE\S
FTWARE\Wow6432Node\Mycompany\MyProj\Model" | ForEach-Object {Get-ItemProperty $_.pspath}
Get-ChildItem : Cannot find path 'C:\Users\Administrator\Desktop\MyDoc\Core\NodeRenameMaster\HKEY_
LOCAL_MACHINE\SOFTWARE\Wow6432Node\Mycompany\MyProj\Model' because it does not exist.
At line:1 char:14
+ Get-ChildItem <<<< "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Mycompany\MyProj\Model" | ForEach-Object {Get-Item
Property $_.pspath}
+ CategoryInfo : ObjectNotFound: (C:\Users\Admini...\Model:String) [Get-ChildItem], ItemNotFound
Exception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Why it occurs ? How to solve this ?
The path is invalid, replace 'HKEY_LOCAL_MACHINE' with 'HKLM:\' and 'SFTWARE' with 'SOFTWARE'