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

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

Related

Zabbix Discovery with External check JSON

Zabbix 3.2.5 in docker on alpine image (official build)
I have some problem with external script and returned JSON.
The script json_data.sh is:
#!/bin/bash
# Generate JSON data for zabbix
declare -i i
fields=$1
data=($2)
json=""
i=0
while [ $i -lt ${#data[*]} ]; do
row=""
for f in $fields; do
row+="\"{#$f}\":\"${data[$i]}\","
i+=1
done
json+="{${row%,}},"
done
echo "{\"data\":[${json%,}]}"
key string is:
json_data.sh["IP", "127.0.0.1 127.0.0.2 127.0.0.3"]
I test it with text item and have result
2539:20170515:095829.375 zbx_popen(): executing script
{"data":[{"{#IP}":"127.0.0.1"},{"{#IP}":"127.0.0.2"},{"{#IP}":"127.0.0.3"}]}
So script returns valid JSON but i still have error Vallue Should be JSON object in service discovery.
What wrong with that JSON?
Template Settings In screenshot {$IPLIST} just macro = "127.0.0.1 127.0.0.2 127.0.0.3"
Error
This is bug. When DebugLevel is more than 3 Zabbix mix part of the debug output with the value data. Something like zbx_popen(): executing script.
Solution to reduce DebugLevel to 3 or lower, and wait until ZBX-12195 will be fixed.

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.

Error when trying to read the .ini file from .nsi script

I have to write a NSIS script for deployment the report on the server.It works fine when I am using localhost.
Now I want to send this package to my client but the problem is that I don't know the SERVER IP of the client for this purpose I have write the .ini file. The content of the ini file is
DeployReport.ini
[SETTINGS]
ServerIp=localhost
UserName=
PassWord=
DeployReport.nsi
# Script generated by DeepSofts - NSIS Script Generator
# Beginning Basic Section Script ...
Name 'DeployReport'
Icon 'modern-install-full.ico'
OutFile 'DeployReport.exe'
SilentInstall Normal
CRCCheck On
Section GetIP 0
ReadINIStr $ip "DeployReport.ini" "SETTINGS" "ServerIp"
MessageBox MB_OK "$ip"
SectionEnd
Section Command 1
Exec '"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\rs.exe" -i PublishSampleReports.rss -s http://localhost/reportserver"'
SectionEnd
ComponentText 'A few details about the application that you have created'
AutoCloseWindow True
SetCompress Auto
SetDateSave On
SetDataBlockOptimize On
The problem is that the DeployReport.nsi script works fine untill I dont use the Section GetIP 0 and I am not able to find the error in the script.
You should use the full path to the .ini ("$exedir\DeployReport.ini")

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

PowerShell remoting vs Host Version

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.