PowerShell optimize-Volume throwing error - powershell

I am creating an automation to optimize the C drive in my environment but when I run the below PowerShell command its throwing an error.(I am running this command on virtual Hard Disk VHDX)
Optimize-Volume -DriveLetter C -Defrag -Verbose
I have checked Both defragsvc (Defragmentation service) and Microsoft Storage SMP services are running. Can any one please help me on this?
Error
Optimize-Volume : Failed
Activity ID: {288b5478-2fd1-4159-8a4f-237d1484c50c}
At line:1 char:1
Optimize-Volume -DriveLetter C -Defrag -Verbose
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified:
(StorageWMI:ROOT/Microsoft/...age/MSFT_Volume) [Optimize-Volume],
CimException
FullyQualifiedErrorId : StorageWMI 4,Optimize-Volume

Related

Trying to use BitsTransfer (Powershell) or bitsadmin to transfer files when booted into WinRE

Trying to use BitsTransfer (Powershell) or bitsadmin to transfer files when booted into WinRE.
I am able to get the file using invoke-webrequest for the same paths. None of the Bits commands give much information other than "The device is not ready". My searches only return information on Windows update.
I have tried restarting services.
When I run Start-BitsTransfer I get:
VERBOSE: Performing the operation "New" on target "BitsTransfer".
Start-BitsTransfer : The device is not ready.
The device is not ready.
At line:1 char:1
Start-BitsTransfer "http://...Source...
+ CategoryInfo : NotSpecified: (:) [Start-BitsTransfer], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand
Get-BitsTransfer command gives me:
get-bitstransfer : The device is not ready.
The device is not ready.
At line:1 char:1
get-bitstransfer
+ CategoryInfo : NotSpecified: (:) [Get-BitsTransfer], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.BackgroundIntelligentTransfer.Management.GetBitsTransfer
BitsAdmin.exe gives me:
BITSADMIN version 3.0
BITS administration utility.
(C) Copyright Microsoft Corp.
Unable to connect to BITS - 0x80070015
The device is not ready.

Error during installing PSWindows Update module

I was going to automate some regular task on client computers using Powershell module. In this way, PSWINDOWSUPDATE is one of bests. But, I receive an error during install that:
PS C:\Users\stackoverflow> install-module -name pswindowsupdate -scope currentuser
PackageManagement\Install-Package : Package 'PSWindowsUpdate' failed to be installed because:
Access to the path 'C:\Users\stackoverflow\AppData\Local\Temp\ipufiq2h\PSWindowsUpdate.dll' is denied.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (PSWindowsUpdate:String) [Install-Package], Exception
+ FullyQualifiedErrorId : Package '{0}' failed to be installed because: {1},Microsoft.PowerShell.PackageManagement
.Cmdlets.InstallPackage
running the command in administrative mode and without -scope switch didn't solve the problem.
Edit1: The problem is solved. But there is another problem. When I call import-module pswindowsupdate following error occurs:
import-module : Errors occurred while loading the format data file:
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml, ,
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml: The file was skipped because of the following validation exception: File
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170..
At line:1 char:1
+ import-module pswindowsupdate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
Use Get-ExecutionPolicy to set it e.g. to RemoteSigned:
Get-ExecutionPolicy -List
Set-ExecutionPolicy RemoteSigned
also see here

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.

Error to execute DSC when create virtual machine in azure

I create in powershell Virtual Machine Windows 8.1 in azure. And Set-Azure DSC. But I see error, during execute DSC:
[10/21/2014 14:17:39.44] Executing: C:\Packages\Plugins\Microsoft.Powershell.DSC\1.3.0.0\bin\install.cmd
[10/21/2014 14:17:47.24] Execution Complete.
#
Execution Output:
Execution Error:
C:\Packages\Plugins\Microsoft.Powershell.DSC\1.3.0.0\bin\install.ps1 : File
C:\Packages\Plugins\Microsoft.Powershell.DSC\1.3.0.0\bin\install.ps1 cannot be
loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ C:\Packages\Plugins\Microsoft.Powershell.DSC\1.3.0.0\bin\install.ps1 -verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
How can I enable security policy during create VM in powershell?

Powershell Hyper-V Scripts won't work? + Auto Installing Windows

So I have a strange problem. When I do commands such as this:
New-VM -Name "MattTest" -MemoryStartupBytes 2GB -BootDevice CD -SwitchName Lab -path c:\VMdocs -NewVHDSizeBytes 40GB –NewVHDPath c:\base.vhdx
Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
Start-VM -Name MattTest
It works fine, However if I make a script I get a TON of errors, for example the exact same thing in a script called PS.ps1 and ran through Powershell I get:
New-VM : The parameter is not valid. No switch can be found by given
criteria. At C:\Users\mrsmith\Desktop\PS.ps1:1 char:1
+ New-VM -Name "MattTest" -MemoryStartupBytes 2GB -BootDevice CD -SwitchName Lab - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Lab:String) [New-VM], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.NewVMCommand
Set-VMDvdDrive : The parameter is not valid. Hyper-V was unable to
find a virtual machine with name MattTest. At
C:\Users\mrsmith\Desktop\PS.ps1:3 char:1
+ Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MattTest:String) [Set-VMDvdDrive], VirtualizationInvalidArgumentExcept ion
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMDvdDriveCommand
Set-VMDvdDrive : The parameter is not valid. No DVD drives are found.
At C:\Users\mrsmith\Desktop\PS.ps1:3 char:1
+ Set-VMDvdDrive -VMName MattTest -Path C:\ISOFiles\Windows2008.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VMDvdDrive], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.SetVMDvdDriveCommand
Start-VM : The parameter is not valid. Hyper-V was unable to find a
virtual machine with name MattTest. At
C:\Users\mrsmith\Desktop\PS.ps1:5 char:1
+ Start-VM -Name MattTest
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MattTest:String) [Start-VM], VirtualizationInvalidArgumentException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.StartVMCommand
Im not sure what's causing this? is it something to do with the module, because I have checked and the Hyper-V cmdlet IS running (considering I can manually type these in and they will be fine)
Also sidequestion: These set of instructions will get up to the "Install Windows XXXX" Screen. Is there any way to Automate THAT process? Or not really (Like make it click next, select a hard drive, install, etc...
Just to make sure, the prompt (e.g 'PS C:\Users\mrsmith\Desktop>') is not a part of the script, right? If so remove it.
As it appears, it is a part of the script because PS is an alias for Get-Process and it tries to pass 'C:\Users\mrsmith\Desktop' as a process name.