Install telegraf agent through Powershell Script - windows

$secpasswd = ConvertTo-SecureString "Password" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("administrator", $secpasswd)
$Path = "$env:userprofile\AppData\Local"
cd "$Path\telegraf"
$installtelegraf = .\telegraf.exe --service install --config "$Path\telegraf\telegraf.conf"
$start_telegraf = telegraf.exe --service start
$net_telegraf = net start telegraf
Start-Process Powershell.exe -Credential $mycreds -ArgumentList $installtelegraf $start_telegraf $net_telegraf
But don't know I am getting errors. I'll use this script for automation process to install it on our clients through Group Policy.
Any kind of help will be appreciated.
Thanks.

Your intent is to define the command lines to invoke later with Start-Process as strings, and for that you must use quoting; e.g.:
# Without enclosure in '...' you would *instantly* execute the command.
$start_telegraf = 'telegraf.exe --service start'
To put it all together:
$installtelegraf = "telegraf.exe --service install --config `"$Path\telegraf\telegraf.conf`""
$start_telegraf = 'telegraf.exe --service start'
$net_telegraf = 'net start telegraf'
Start-Process Powershell.exe -Credential $mycreds -ArgumentList #"
$installtelegraf
$start_telegraf
$net_telegraf
"#
Note:
The $installtelegraf = ... assignment uses an expandable (double-quoted) string ("..."), so as to ensure that expansion (string interpolation) takes place, i.e. so that $Path is expanded (replaced with its value); the embedded " chars. must therefore be escaped as `".
Since the other assignments do not require expansion, verbatim (single-quoted) strings ('...') are used.
Note the use of an expandable here-string in the Start-Process call, to make it easy to pass the three variables as separate statements; alternatively, you could have used a single-line string with ; as the statement separator.
Note that I've removed .\ from the first telegraf.exe call, as it doesn't appear in the second.
Generally, note that the target user must have permission to access the caller's working directory. If not, a different one must be specified via -WorkingDirectory.

Related

Powershell - Start-Process ArgumentList accepts only single variable with spaces

I'm trying to start my script from Explorer. I've found the solution and it works if script doesn't have any parameters.
$file = [System.IO.Directory]::GetCurrentDirectory() + "\Trees.ps1"
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File `"$($file)`""
However, if I add additionall parameters just like the first it ceases to work. Ie. this code throws "The string is missing the terminator: '." error.
$file = [System.IO.Directory]::GetCurrentDirectory() + "\Trees.ps1"
$Context = [System.IO.Directory]::GetCurrentDirectory()
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -Context '"$($Context)'" -File `"$($file)`""
I'm using this way of expecting variable:
[Parameter(Mandatory=$True)]
[string]$Context,
What can I do to pass more then one variable with spaces in ArgumentList?
I suspect that I should pass arguments for file content in other way than when just passing file name, but couldn't find solution.
In addition to the back ticks issue that Theo noted, -context should follow -file.
-File
Runs the specified script in the local scope ("dot-sourced"), so that the
functions and variables that the script creates are available in the
current session. Enter the script file path and any parameters.
File must be the last parameter in the command, because all characters
typed after the File parameter name are interpreted
as the script file path followed by the script parameters.
So your command line would be
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File `"$($file)`" -Context $($Context)"
Since you are using an external process call, you will need to use inside double quotes instead of single quotes. You can escape double quotes simply by adding another double quote ("").
Start-Process powershell -verb runas -ArgumentList "-ExecutionPolicy UnRestricted -File ""$file"" -Context ""$Context"""

Use PowerShell to create a shortcut to launch a PowerShell script

I have a need to create a shortcut on a Windows 2019 Server for the Eclipse application so that it runs a PowerShell script instead of opening Eclipse. The script runs a few commands first and then opens the Eclipse application. When I edit an existing shortcut for Eclipse, I can modify the target to be:
"powershell.exe -ExecutionPolicy bypass C:\temp\eclipse-fix.ps1 -WindowStyle Hidden"
This works fine when completed manually. However, when I try to do the same with a PowerShell script, to automate the process, I get an error.
Contents of the script:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Users\Administrator\Desktop\Eclipse.lnk")
$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy bypass C:\cfn\temp\eclipse-fix.ps1 -WindowStyle Hidden"
$Shortcut.IconLocation = "%SystemDrive%\eclipse\eclipse.exe"
$Shortcut.Save()
Error Returned:
PS C:\Users\Administrator> $WshShell = New-Object -comObject WScript.Shell
PS C:\Users\Administrator> $Shortcut = $WshShell.CreateShortcut("C:\Users\Administrator\Desktop\Eclipse.lnk")
PS C:\Users\Administrator> $Shortcut.TargetPath = "powershell.exe -File C:\cfn\temp\eclipse-fix.ps1"
Value does not fall within the expected range.
At line:1 char:1
+ $Shortcut.TargetPath = "powershell.exe -File C:\cfn\temp\eclipse-fix. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
PS C:\Users\Administrator> $Shortcut.IconLocation="%SystemDrive%\eclipse\eclipse.exe, 0"
PS C:\Users\Administrator> $Shortcut.Save()
Result:
The icon is created but has no value for target.
Does anyone know what I am missing to get this to work? Is there another option to use that I may not be aware of?
Thanks.
You need to specify the Arguments separate from the TargetPath
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Users\Administrator\Desktop\Eclipse.lnk")
$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$Shortcut.Arguments = "-ExecutionPolicy bypass C:\cfn\temp\eclipse-fix.ps1 -WindowStyle Hidden"
$Shortcut.IconLocation = "%SystemDrive%\eclipse\eclipse.exe"
$Shortcut.Save()
You can also use just powershell.exe in place of the full path. Either will work.
Here is a script I wrote that will create a shortcut to itself on the desktop that is executable (Can run by double clicking) when run.
I did this to take the step-by-step process out of colleagues having to create their own shortcuts of my powershell automation scripts.
Because our shared drive at work is a different location for everybody, I use the "Get-location" command which gets the pwd, then trim it and parse it for use within the shortcut's argument field.
In order to have the arguments contain double quotes, you have to use the escape character ` preceding another set of quotes, this will allow for the script to send quotes to the arguments field. It will not work without this. '" '"
Make sure that when you are running the script you are not running it from the powershell IDE, or the file paths will be wrong. You need to save the script in your target source path and the nrun it from there by either right ckicling it and running it as a powershell script or creating an executable shortcut for it. Let me know if you need instructions on how to create an executable powershell shortcut.
Hope this helps!
#Grap the pwd
$location = Get-Location | Select Path | ft -HideTableHeaders | Out-String;
#Trim the pwd variable of any extra spaces and unwanted characters
$location = $location.Trim();
$locations = $location.replace(' ' , '');
$locations = $locations.replace("`n","");
#Make the source file location my path to powershell.exe
$SourceFileLocation = "`"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`"";
# Declare where I want to place the shortcut, I placed it on the desktop of whomever is running the script with the dynamic $env:USERNAME which takes the username of whomever is running the script - You can name the shortcut anything you want at the end as long as it ends with .LNK
$ShortcutLocation = “C:\Users\$env:USERNAME\OneDrive\Desktop\PopupSortcut.lnk”;
#Create a now com
$WScriptShell = New-Object -ComObject WScript.Shell;
#create shortcut and provide the location parameter as an argument
$Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation);
#set the target path
$Shortcut.TargetPath = $SourceFileLocation;
#Add arguments that will bypass the execution policy as well as provide a path to the source powershell script (make sure that the entire argument has double quotes around it and that the internal quotes have escape characters (`) behind them, these are not apostrophes but back ticks, located on the same key as the tilde (~) key on the keyboard
$Shortcut.Arguments = “-ExecutionPolicy Bypass -file `"$locations"+"Pop-up.ps1`"”;
#Save the Shortcut
$Shortcut.Save();
########################### This is the meat of our script Performing an Operation #######################
$wshell = New-Object -ComObject Wscript.Shell;
$wshell.Popup("Operation Completed",0,"Done",0x1);

How to pass powershell variable value to cmd.exe?

I am trying to install a service 'ServiceName' from the command prompt, with parameter values which I am taking as user input forms through powershell.
$server = $return[0]
Invoke-Expression -Command "cmd.exe /c ServiceName --install --server=$server"
Basically the ServiceName service takes parameter values from server variable while installing. I am not exactly sure if this is the correct way of using powershell variable values in cmd.exe. Can the cmd.exe take in the server variable value in the manner shown above?
You can add new services in Powershell, and you dont have to use cmd for that:
$serviceName= Read-Host("Enter service name")
$mycreds = Get-Credential
$binaryPath = Read-Host("Enter service executable path")
New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $mycreds
Also you can use
Get-Help New-Service
If you want additional parameters passed to the command. Or if you want, you can parse if from a single input, but its more messy.

How to condense PowerShell script to fit on a single line

Quick question. I am trying to write the following PowerShell script, but I would like it to fit on a single line:
$app = New-Object -comobject Excel.Application
$wb1 = $app.Workbooks.Open("C:\xampp\upload_files\Launchpad.xlsm")
$app.Run("Refresh")
$wb1.Close($false)
$app.Quit()
The pseudo-code would look something like this:
$app = New-Object -comobject Excel.Application AND $wb1 = $app.Workbooks.Open AND "C:\xampp\upload_files\Launchpad.xlsm") AND $app.Run("Refresh") AND $wb1.Close($false) AND $app.Quit()
The reason I want to fit on a line is because I would like to insert the arguments directly in the 'arguments' box of Windows Task Scheduler. The reason for this is that for some reason scripts have been disabled (e.g. I cannot call a .ps1 file...)
I know this will still work, as I already have a "one liner" PS script running. What would the syntax look like??
Kind regards,
G.
Powershell statements can be separated with semicolons:
$app = New-Object -COM 'Excel.Application'; $wb1 = $app.Workbooks.Open("..."); ...
The PowerShell executable takes a -Command parameter that allows you to specify a command string for execution in PowerShell:
powershell.exe -Command "stmnt1; stmnt2; ..."
To run this via Task Scheduler you'd put powershell.exe into the "program" field and -Command "stmnt1; stmnt2; ..." into the "arguments" field of the task.
However, as #alroc said: you should verify why script execution has been restricted. If it's just the default setting you can simply change it by running Set-ExecutionPolicy RemoteSigned or override it by adding -ExecutionPolicy ByPass to a PowerShell command line. However, if the setting is enforced by policy changing/bypassing the setting will fail, and you could get into quite some trouble for violating company policies.
Here is a solution that you might use if the script is not that easy to convert, but you are on Windows running at least PowerShell V5.
It converts the code into Base64 and uses PowerShell.exe with the parameter -encodedCommand to pass the encodedCommand as string.
$command = Get-Content .\YourPowerShellFileContainingTheCode.ps1 -raw
# Get-Content may require "-encoding utf8" or other encodings depending on your file
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))
Write-Output "Text for application:"
Write-Output "PowerShell.exe" ""
Write-Output "Text for argurments:"
Write-Output "-encodedCommand $encodedCommand"
It would look like this, but with a much larger command:
Text for application:
PowerShell.exe
Text for argurments:
-encodedCommand SABvACAASABvACAASABvACwAIABzAHQAYQBjAGsAbwB2AGUAcgBmAGwAbwB3AA==

1734: The array bounds are invalid

I have a script in which I need to run one command as an administrator. When I ran this command the script errors with a 1734 error.
My script is very basic:
runas /user:Administrator "myexec.exe \"param with spaces\" otherparam -Djava.ext.dirs=%JAVA_EXT_DIRS%"
The problem comes from the variable JAVA_EXT_DIRS which is kind of huge.
This is an old question, but I've ran into the same problem on Windows 10 with the runas command now. It turns out there's a maximum length for the program parameter, which has to be below 995 characters.
For example, this command still works:
runas /user:someuser /savecreds "cmd.exe 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
The program parameter here has 994 characters, and it should open a new command prompt. But if you add one more 1 within that parameter, the execution will fail with a 1734: The array bounds are invalid. error.
And if you increase the the program parameter even further to 1026 characters, the error changes to -2147024809: The parameter is incorrect..
The regular limit for command line parameters seems to be much much larger (I've read something about 8191 characters here on SO), so this seems to be a problem with runas.exe itself.
Edit:
I even ran into a similar problem when I tried to use a PowerShell script with the -Credential flag like this:
$username = "username"
$password = "password"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential $username, $securePassword
$argument = $args[0]
Start-Process -FilePath "C:\path\to\my.exe" -Credential $credentials -ArgumentList "-arg $argument"
So it's probably a problem with the underlying Windows mechanics and not runas.exe itself.
The value of %JAVA_EXT_DIRS% may contain spaces too. You better put it in double quotes:
runas /user:Administrator "myexec.exe \"param with spaces\" otherparam -Djava.ext.dirs=\"%JAVA_EXT_DIRS%\""

Resources