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

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.

Related

Running runas/psexec in powershell remote session is not working

I have a remote machine where PS-Remoting enabled and I am able to run commands and scripts from my client/workstation. I want to run certain tasks as different user, in other words not the same user as I connect the remote-session.
Why I am doing this ?
I am using puppet-bolt for remote execution, where I can re-use some already existing puppet modules. And this bolt tools uses WinRM.
Issue I am facing is, while executing the below command directly on the remote machine works fine. I am using jetbrains runas utility.
# Executing this command directly on the remote machine's powershell prompt works.
C:\agent\tools\custom_runas.exe '-u:ad_domain\username' '-p:password' C:\Windows\System32\HOSTNAME.EXE
If I execute the same command over PS-RemoteSession (or) Invoke-Command gives me same error again. User which I use to connect WinRM service is an administrator user.
PS C:\WINDOWS\system32> Invoke-Command -ComputerName $CName -Credential $Crdn -ScriptBlock { C:\agent\tools\custom_runas.exe '-u:ad_domain\username' '-p:password' C:\Windows\System32\HOSTNAME.EXE }
JetBrains RunAs x86 1.0.0.061
Copyright (C) 2017 JetBrains. All rights reserved.
Runs a process under the specified windows user account.
Argument(s): -u:ad_domain\username -p:***** C:\Windows\System32\HOSTNAME.EXE
Settings:
user_name: username
domain: ad_domain
working_directory: C:\Users\UserName\Documents
exit_code_base: -100000
integrity_level: auto
inheritance_mode: auto
show_mode: hide
self_testing: 0
executable: C:\Windows\System32\HOSTNAME.EXE
NotSpecified: (:String) [], RemoteException
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : remotemachine
NotSpecified: (:) [], RemoteException
Error: Access is denied.
CreateProcessWithLogonW returns the Win32 error 0x00000005.
command_line_args:
Some observations, running "runas" command on the PSRemoteSession session doesn't wait for entering password and I also tried using PsExec.exe apart of JetBrains runas which also has the same behavior.

How to run PowerShell Invoke-Command from Command Prompt when the script has a switch?

I have the need to run PowerShell scripts using the command prompt (calling powershell.exe with the -c parameter). I have run these for years but have never tried it with Invoke-Command in order to do remoting and when the script I want to execute has a switch I have to pass.
This piece of code works great from PowerShell, a simple script that has the LogRun switch:
icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}
However, when I run it via the Command Prompt with powershell.exe fails:
powershell -c "icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}"
I have tried many variations of this and nothing works, I am sure I missing some syntax detail or some quotes somewhere.
Please let me know if you can help me figure this out.
This is the error I get:
icm : The value of the using variable '$using:LogRun' cannot be retrieved
because it has not been set in the local session.
At line:1 char:1
+ icm -cn MYCOMPUTER02{D:\PatV2DU\PatV2DUService20180411120032\Sc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Command], RuntimeException
+ FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand

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.

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

Powershell stop-service error: cannot find any service with service name

I'm working on a script to deploy vendor software to a large environment. The first step is to stop the services in question. The script executes fine in our test environment, but I'm not an admin in the production environment so I'm convinced it's a permissions issue. I can't get admin rights to the prod environment so I need to try to find out anything that I may need to set to grant permissions to stop services remotely. I'm issuing the following command to stop services:
Stop-Service -InputObject $(Get-Service -Computer $destination.Server -Name ("moca."+$destEnv))
When I run the script I get:
Cannot find any service with service name 'moca.WMSPRD'.
+ CategoryInfo : ObjectNotFound: (moca.WMSPRD:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StopServiceCommand
The service definitely exists and if I rdp into the target box and issue the stop-service command locally it will execute. So there is something preventing me from stopping the service remotely. Any ideas?
Edit:
A coworker suggested using WMI so tried replacing the Stop-Service line with:
(Get-WmiObject -computer $destination.Server Win32_Service -Filter ("Name='moca."+$destEnv+"'")).InvokeMethod("StopService",$null)
and I get:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
If you know the exact service name you can try this
(Get-WmiObject -computerName $_.name Win32_Service -Filter "Name='moca'").StopService()
Here im assuming that the service name is moca
Is DCOM working on the remote computer? I know how to do it with remote powershell, which uses wsman:
invoke-command comp001 { stop-service adobearmservice }