Copy-Item throws Null reference exception - powershell

I am trying to copy a file from Guest Linux VM to Host Hyper V windows machine.
I am using powershell to copy the file from guest vm(linux) to Host(windows).
Write-Host "$session, $TestLogPath, $destLogs" #all variables are valid and not null
Copy-Item -FromSession $session -Path $TestLogPath/Test.txt -Destination $destLogs
I get the following error while doing that:
Object reference not set to an instance of an object.
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : 10.218.10.13
I am not sure what this error is since the desired file is copied successfully from Linux VM to Windows Host.
Any one has any idea why this error is shown despite file copied successfully from Linux VM to Host machine.

Related

How to run Start-Job to bring up vagrant VM's separately

I am very new to Powershell, but through looking at several other related questions I've put together the below block of code:
Get-Content .\test-vagrant-box-list.txt | %{
Start-Job -ScriptBlock { param($boxName) vagrant up $boxName } -Argument $_
}
My goal is to start four vagrant VM's in parallel through powershell. I run this script and it executes and starts each job, however it gives me the below error when I run "Receive-Job ":
The machine with the name 'worker1' was not found configured for
+ CategoryInfo : NotSpecified: (The machine wit... configured for:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : localhost
this Vagrant environment.
NotSpecified: (:) [], RemoteException
These VM names are definitely defined and match up to what I have in my Vagrantfile. I made sure if i run the command "vagrant up " that they begin to build. Only through this powershell script it seems it can't find the VM's defined with that name though.
** I have looked at the -parallel flag for vagrant but in my case it doesn't seem to actually work. This was my solution instead.

Error Creating New VMs using PowerShell

I'm struggling to create a new virtual machine on Hyper-V using a simple example from the help file. I can create VMs manually using the GUI but I was trying to learn how to do this using PowerShell but I'm getting the error below.
New-VM -Name "base" -MemoryStartupBytes 1GB -NewVHDPath "J:\Hyper-V machines\dc.vhdx" -NewVHDSizeBytes 100GB
Error Message:
New-VM : Failed to create a new virtual machine. 'base' failed to realize.
(Virtual machine ID 80967A7D-7380-4771-B200-99129D63496C) Failed to access
configuration store: The device is not ready. (0x80070015).
At line:1 char:1
+ New-VM -Name "base" -MemoryStartupBytes 1GB -NewVHDPath J:\VM\base.vh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], VirtualizationExcept ion
+ FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.NewVM
My VMs are located in an external harddrive and the path exists ...there's not much help about this error online.

Hyper-v Export-VM : Provider load failure error in 2012r2

I'm using powershell 4 to export all the VMs in hyper-V and move it to a backup server(nas unix). Export was successful for few VMs and after a while it fails with the following errors:
Export-VM : Provider load failure
At C:\scripts\test.ps1:37 char:5
Export-VM -Name $vmname -Path $ExportPath
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Export-VM], ManagementException
FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.ExportVMCommand
This is the error i get in event viewer:
The description for Event ID 18350 from source Microsoft-Windows-Hyper-V-VMMS cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
71706037-090D-4B74-BB0B-AEE96D47D898
%%2147942432
0x80070020
The locale specific resource for the desired message is not present
The same script works fine in windows 2016 server running powershell 5. I'm not sure if this is OS specific or it has to do with any updates. Please share your view on this and help me resolve this issue.
Update
I also get this Error:
Get-VM : Quota violation
At line:1 char:1
+ Get-VM
+ ~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-VM], ManagementException
+ FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.GetVMCommand
This issue occurred because Hyper-V service became unresponsive. Restarting Windows Management Instrumentation service or hyper-v service fixed this issue.

Remote registry query

I am trying to get a registry value from a remote machine but everything I have tried did not work.
Both machines are running windows 7 x64, they are on the same domain, firewall off, powershell 4.0
when I run :
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", "APPS-EUAUTO1")
$key = $reg.OpenSubkey('SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion')
Write-Host $key.GetValue('InstallDate')
i get
Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
"
At C:\Users\User\Desktop\test.ps1:1 char:1
+ $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", "APPS-EU ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException
I know nothing of .Net and I have only played with PowerShell for the last month.
I have also tried the module PSRemoteRegistry and when I am using Get-RegValue I get the same error message (I think it is using the same thing)
Assuming you can ping APPS-EUAUTO1(and it resolves to correct IP) and firewall is off.. make sure Remote Registry service is running on your APPS-EUAUTO1

Cannot Copy a file from a network share in powershell until I open the share in windows explorer

I have a very simple powershell script which to copy files from a networked server.
here's the script
Write-Output "Copying Backups"
copy \\sqlbox\SqlBackups\Client.bak c:\sqlbackups
It throws this exception
Invalid Path: '\\sqlbox\SqlBackups\Client.bak'.
At C:\Work\db\restore.ps1:5 char:1
+ copy \\sqlbox\SqlBackups\Client.bak c:\sqlbackups
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], ArgumentException
+ FullyQualifiedErrorId : CopyItemDynamicParametersProviderException
If I open a windows explorer up and goto \sqlbox\SqlBackups it opens fine and if I then run the powershel script it also starts working and will work until the next time I reboot the PC.
How can I address this in the script?
Try mapping the drive first, then carry out the copy operation.
New-PSDrive -Name S -Root \\sqlbox\SqlBackups\ -PSProvider FileSystem
Write-Output "Copying Backups"
copy S:\Client.bak c:\sqlbackups
I have a similar problem when copying files over servers with this error:
Cannot find path '\\computer1\d$\path' because it does not exist.
It works after adding Microsoft.PowerShell.Core\FileSystem:: in front of file name:
copy-item "Microsoft.PowerShell.Core\FileSystem::\\computer1\d$\path\installer.msi" "Microsoft.PowerShell.Core\FileSystem::\\computer2\d$\path\installer.msi"