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

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.

Related

PowerShell PackageManagement - How to uninstall a package provider?

Troubleshooting i tried uninstalling nuget. NuGetProvider-2.8.5.208.dll to be specific.
I have never worked with Power Shell before and am totally lost.
I found this question that helped me find nuget and create a command to delete it.
But I'm unable to follow through.
I did start powershell as administrator.
Here the input:
(Get-PackageProvider|where-object{$_.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
The error message:
Remove-Item : The input object cannot be bound to any parameters of the command because the command does not accept pipeline input or the input and its properties do not match any of the parameters that accept pipeline input.
and its properties do not match any of the parameters that accept pipeline input.
line:1 character:70
+ ... _.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Users\XXX ...GetProvider.dll:String) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.RemoveItemCommand
How can i solve this and successfully uninstall it?
Thanks for your answer!
Still no luck. Here the error:
Remove-Item : The element C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
\Microsoft.PackageManagement.NuGetProvider.dll cannot be removed: Access to the path "C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvide
r.dll" denied.
Line:1 Character:44
+ (Get-PackageProvider NuGet).ProviderPath | Remove-Item -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\XXX ...GetProvider.dll:FileInfo) [Remove-Item], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

Installing CCM client on PC via powershell

I'm trying to speed up how soon SCCM recognises a device on the domain and starts the process of installing applications.
I have the below in powershell
$CCMInstallerPath = "\\ServerFQDN\SMS_MAN\Client\ccmsetup.exe"
$args = "/mp:MPServer", "/LOGON", "/UsePKICert", "SMSSLP=SMSSLPServer", "SMSSITECODE=MAN"
& $CCMInstallerPath $args
I get the below error:
Program 'ccmsetup.exe' failed to run: Access is deniedAt line:1 char:5
+ & $CCMInstallerPath $args
+ ~~~~~~~~~~~~~~~~~~~~~~~~~.
At line:1 char:5
+ & $CCMInstallerPath $args
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
I have read and execute access to this file, both by group and directly by name
I have also tried:
Start-Process -FilePath \\ServerFQDN\SMS_MAN\Client\ccmsetup.exe -ArgumentList "/mp:MPServer", "/logon SMSSITECODE-MAN", "/UsePKICert"
and get the error
Start-Process : This command cannot be run due to the error: Access is denied.
At line:1 char:5
+ Start-Process -FilePath \\ServerFQDN\SMS_MAN\Client\c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
ServerFQDN is a replacement for one of our NM servers
I can fully connect to the folder and run the exe manually
I am running elevated powershell
Issue was caused by incorrect share permissions found in effective access.
Permission was granted to run the file but effective access was showing differences

Issue with New-DFSNFolderTarget cmdlet

I'm trying to automate repointing DFS folders. This is my current script:
$ft=get-dfsnfolder "\\test.dfs.domain\testpath\zzTop\*" | get-dfsnfoldertarget
$ft|% {
if ($_.targetpath -like "*blah*") {
new-dfsnfoldertarget -Path $_.path -State online -TargetPath ($_.targetpath).replace("\\blah.test.domain\testpath\zzTop","\\new.test.domain\testpath\zzTop")
}
}
I'm receiving a very weird error that is completely non-specific and makes this basically impossible to debug below:
new-dfsnfoldertarget : Can't add DFS namespace folder target "\\blah.test.domain\testpath\zzTop\2two"
At C:\Users\my_username\Desktop\ChangeDFSTargetTEST.ps1:2 char:46
+ ... *blah*") {new-dfsnfoldertarget -Path $_.path -State online -TargetP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_DfsNamespaceFolderTarget:ROOT\Microsoft\...aceFolderTarget) [New-DfsnFolderTarget], CimException
+ FullyQualifiedErrorId : Windows System Error 80,New-DfsnFolderTarget
new-dfsnfoldertarget : A general error occurred that is not covered by a more specific error code.
At C:\Users\my_username\Desktop\ChangeDFSTargetTEST.ps1:2 char:46
+ ... *blah*") {new-dfsnfoldertarget -Path $_.path -State online -TargetP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_DfsNamespaceFolderTarget:ROOT\Microsoft\...aceFolderTarget) [New-DfsnFolderTarget], CimException
+ FullyQualifiedErrorId : MI RESULT 80,New-DfsnFolderTarget
I'm running this script as admin on the box that is hosting and managing the DFS, so no issues with remote connections. I also have a script to offline these paths which works fine and also uses the "$_.targetpath -like "blah".
Also, if I run the command:
new-dfsnfoldertarget -Path $_.path -State online -TargetPath ($_.targetpath).replace("\\blah.test.domain\testpath\zzTop","\\new.test.domain\testpath\zzTop")
Replacing the variables with the strings that are fed into it automatically (so running it manually), it works fine. This is from a Server 2019 Standard box with PSVersion 5.1.17763.1490.
Any insight would be appreciated.
This won't be terribly relevant, but I realized I had set up DFS using an admin share path which is locked down almost completely so the testing I was doing was useless.
This script works fine if your DFS is healthy.

Creating computer objects without accessing ARS console

I want to create computer objects without accessing the ARS console
powershell.exe -command New-QADComputer -Name <Name of the new computer> -ParentContainer "<OU-Copied the distinguishedName of an earlier computer object from the AD>" -ObjectAttributes #{edsajoincomputertodomain='domain\userID'}
I am getting the below error while executing the above command.
New-QADComputer : Cannot convert 'System.Object[]' to the type 'ActiveRoles.ManagementShell.Data.IdentityParameter'
required by parameter 'ParentContainer'. Specified method is not supported.
At line:1 char:49
+ ... ntContainer <OU> ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-QADComputer], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,ActiveRoles.ManagementShell.Powershell.Cmdlets.NewComputerCmdlet
Note: We need to use the same credential which we used to add the computer object in AD to join the client computer to Domain.

Powershell not always running ScriptsToProcess on import

After using Install-Module to install powershell-yaml I had about a week of use before I came in one morning to it not loading YamlDotNet properly.
Now if I try to use ConvertFrom-Yaml before manually running Import-Module powershell-yaml I get the the following error:
PS C:\Users\user> "---" | ConvertFrom-Yaml
New-Object : Cannot find type [YamlDotNet.RepresentationModel.YamlStream]: verify that the assembly containing this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:24 char:23
+ ... $yamlStream = New-Object "YamlDotNet.RepresentationModel.YamlStream"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:25 char:9
+ $yamlStream.Load([System.IO.TextReader] $stringReader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Previously (and on most systems) this worked fine. If I run a manual Import-Module before calling the function it works fine:
PS C:\Users\user> Import-Module powershell-yaml
PS C:\Users\user> "---" | ConvertFrom-Yaml
PS C:\Users\user>
Both my and a coworker's machines started this behavior at about the same time. This morning my system started working properly again, but his is still exhibiting the behavior. We haven't been able to duplicate it on other machines.
I've narrowed it down to a script referenced in Powershell-Yaml manifest's ScriptsToProcess not being called during autoload, but runs fine during a manual Import-Module. On working machines the script in ScriptsToProcess is run in both cases. As a workaround we can force a module load by putting an Import-Module in our profile, ideally we'd like to find a root cause.
Import-Module -Verbose doesn't help, because calling Import-Module first always works.