PowerShell remoting vs Host Version - powershell-remoting

I have a strange issue using remoting with Powershell.
I have a script called dothis.ps1 which does the following:
$s = New-PSSession -ComputerName $someServer -Authentication CredSSP -Credential $credential
$result = Invoke-Command -Session $s { param($dropLocation) C:\somScript.ps1 #PSBoundParameters } -ArgumentList $DropLocation
This script is executed by the InvokeProcess of Microsoft.TeamFoundation.Build.Workflows.Activities
The issue I have is that if I print out the version of Powershell from the dothis.ps1, I have 2.0.
But if I print out the version of the Powershell from the remote script (somSecript.ps1), it gives me 1.0.
I wanted to know the version of the powershell I was running because the following command does not work: $Host.Runspace.ThreadOptions
Can somebody explain me what is going on? Thanks.

The version value you're reading comes out of the registry apparently, even though you're on a newer version of PS (since PS v1 doesn't support sessions). Reading this other StackOverflow article pointed me to $psversiontable, which should get you exactly what you're after.

Related

Windows PowerShell command that changes text in a file

I need to change the my.ini file in my MySQL setup. I want to increase the max_connections. So basically have a .bat file that calls some powershell commands that replace the text file in a file and save it.
I am trying to do this by running a .bat file that will run a powershell command to change the text, so far this has been a huge headache. I have tried several solutions and it either doesn't give me the permissions to replace the text or it tells me that the object cannot be used in the piped command.
I managed to get it working in the admin powershell without using a .bat file but it won't let me do it in non admin and in a .bat file.
I tried this below
PowerShell -NoExit -Command "(Get-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini') | ForEach-Object { $_ -replace \"max_connections=[0-9]+$\", \"max_connections=10000\" } | Set-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini' -"
and get the following error
Set-Content : The input object cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
I have also tried this variation using the admin powershell call
PowerShell -NoExit -Command "Start-Process powershell -verb runas -ArgumentList \"-NoExit -Command (Get-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini') | ForEach-Object {{ $_ -replace \""max_connections=[0-9]+$\"", \""max_connections=10000\"" } | Set-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini' -\""
but I get the following error
PowerShell -NoExit -Command "Start-Process powershell -verb runas -ArgumentList \"-NoExit -Command (Get-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini') | ForEach-Object {{ $_ -replace \""max_connections=[0-9]+$\"", \""max_connections=10000\"" } | Set-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini' -\""
'ForEach-Object' is not recognized as an internal or external command,
operable program or batch file.
I have looked at several stack overflow questions regarding this but I cannot seem to get it to work.
If there is an easier way get the solution done then I am open to suggestions.
UPDATE:
When I copy and paste the solution or get another program to pass it to powershell.exe I get these weird square brackets in the command, why does this happen? (I use innoscript to call powershell.exe)
Being able to do it in a batch file should just be getting your syntax right. Try this:
PowerShell -NoExit -Command {(Get-Content 'C:\ProgramData\MySQ‌​L\MySQL Server 5.7\my.ini') | ForEach-Object { $_ -replace "max_connections=[0-‌​9]+$", "max_connections=100‌​00" } | Set-Content 'C:\ProgramData\MySQ‌​L\MySQL Server 5.7\my.ini'}
As for not having to run in Admin, check the security tab on your file and make sure your user account has rights to it, and not just people in the Administrators group.

Deploy Azure VM from visual studio using json template?

I'm trying to deploy a VM from Visual Studio using a json template. Before i was using the New-AzureResourceManagerDeployment where I could specify "-TemplateFile "$PSScriptRoot\TemplateName.json". That cmdlet is no longer avalible and I can't seem to find an answer anywhere.. What do you suggest I use instead?
It sounds like you may have an older deployment script and have since updated your Azure PowerShell cmdlets that use the new "AzureRM" commands. Assuming that's the case, you will need to update your script to use the new "AzureRM" commands. For example, instead of New-AzureResourceManagerDeployment use New-AzureRmResourceManagerDeployment.
Here is an example of how these commands are used in today's version of the deployment script that Visual Studio generates
... <abbreviated to show just the last two Azure PowerShell commands ...
#Create or update the resource group using the specified template file and tem plate parameters file
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $ResourceGroupLocation -Verbose -Force -ErrorAction Stop
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParametersFile `
#OptionalParameters `
-Force -Verbose

How to run the Powershell script file in remote server by passing the parameters

I am facing issue to run the powershell script file in remote server. I am having the script file in remote server which is having 3 parameters. I need to call and execute this file from the lab server using Invoke-command.
ex: Invoke-Comamnd -session $s -Scriptblock{$filePath $arg1 $arg2 $arg3 }
Thanks in advance
try something like
$remoteCommand = "LocationOfScript\Script.ps1 -ParamOne $ValueOne - ParamTwo $ValueTwo"
Invoke-Command $remoteCommand

How to check if Google Chrome is installed on Windows machine using Inno Setup

I am developing an installer using Inno Setup and I need to find whether Google Chrome is installed in the machine.
I have found answers that say that I can check at the following path in the registry,
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome
But this didn't solve my problem. I don't have this path in my registry.
Can anyone help me?
This will help:
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo
I Know this is an old thread, however, building off of Walt's answer here is the full solution that I came up with:
$chromeInstalled = (Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo
if ($chromeInstalled.FileName -eq $null) {
Write-Host "Chrome is not installed"}
else {
Write-Host "chrome is installed"
}
I found a way easier way to do this.
$chrometest = Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
if($chrometest -eq $true){
Write-Host "Chrome is installed"
}else{
Write-Host "Chrome is not installed"
}

Powershell script works in Powershell but fails in Task Scheduler

I have a PowerShell script that sends an email via SMTP. The script runs fine inside Powershell ISE, but fails in Task Scheduler. I am on Windows Server 2012. I have other Powershell scripts that I run on this server using the exact same setup, but those scripts do not send an email. The return code I see in Task Scheduler is (0xFFFD0000) and I cannot find any information on this. I have the task set to run with highest privileges and I have checked that the executionpolicy is RemoteSigned. Anybody run into this before?
Here is the command in the task:
powershell -f "c:\scripts\EmailTest.ps1"
Here is the script:
$EmailFrom = "user#domain.com"
$EmailTo = "someone#somewhere.com"
$Subject = "Email Subject"
$Body = #"
Person,
Some message here
Thanks,
User
"#
$SMTPServer = "smtp.domain.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user#domain.com", "password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Update:
I was able to resolve the issue. Apparently I had an additional line in the script that was commented out. I'm not sure why this would cause an error but once I removed that commented out line it ran fine in Task Scheduler. the comment looked like this and was just below the other $EmailTo declaration in the above script:
#$EmailTo = "someone#somewhere.com"
I found another possible issue while looking at a similar problem. I was unable to execute a PowerShell script as a Task Scheduler action, even though the script ran correctly when logged into Windows as the target user and running within PowerShell.
Task Scheduler would consistently display the 0xFFFD0000 error when I nominated the script in the task's action arguments using what I believed to be normal PowerShell quoting rules:
-ExecutionPolicy Bypass -File 'D:\full path\to\script.ps1'
PowerShell acquiesced and Task Scheduler fired off the task immediately and without issue when I changed the quotes I used from single to double:
-ExecutionPolicy Bypass -File "D:\full path\to\script.ps1"
Dropping to a command prompt and executing the full command immediately revealed the problem:
D:\>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File 'D:\full path\to\script.ps1'
Processing -File ''D:\full path\to' failed: The given path's format is not supported. Specify a valid path for the -File parameter.
Notice the strange use of two single quotes before the path and one single quote after.
The moral of the story: When feeding the full path of a script to PowerShell as a command line parameter, use double quotes!
I was receiving the same error and ultimately I had a different issue - the optional start in directory setting wasn't applied.
Essentially, I was running a .bat file - c:\tasks\process.bat
This .bat file referenced multiple ps1 scripts that were in the tasks directory and the references were just by file name (not the full directory). On the action tab in task scheduler, there is a Start in (optional) field that I had not populated. Setting it to c:\tasks allowed the references to function properly.
First of all you have to check "ExecutionPolicy" configured on your machine. to do so, check default values by following this link https://technet.microsoft.com/en-us/library/hh847748.aspx
I fixed my probleme by using this command:
On "Add arguments" option I put:
"-Executionpolicy Bypass -command "& 'T:\deleteOldMessages.ps1' "
and