Running program from fileshare in powershell - powershell

What I need to do is execute a program on a remote fileshare inside powershell. An example path would be:
\mycompany\filesharename\folder\program.exe
This program takes a command line arguement, a string, and decrypts it. Problem is I need to decrypt and encrypt literally thousands of lines from different files on different computers. Doing it one at a time through that thing is driving me mad. I've started out with this:
clear-host
([string]$step='this')
$value = Invoke-Command '\\mycompany\fileshare\folder\software\program.exe' $step
write-host $value
This is throwing an error:
The term '\\mycompany\fileshare\folder\software\program.exe' is not re
cognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\Me\Documents\Scripts\test.ps1:3 char:77
+ $value = \\mycompany\fileshare\folder\software\program.exe <<<< $st
ep
+ CategoryInfo : ObjectNotFound: (\\mycompany\fileshare...\program.exe:String
) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Eventually the plan is to code a gui, have the script grab the strings I need from the files themselves, and print them inside a local folder. Right now I need to figure out how the heck to use that progam inside my script. Any help?

Use Invoke-Expression instead of Invoke-Command:
$step='this'
$value = Invoke-Expression "\\mycompany\fileshare\folder\software\program.exe $step"
write-host $value

Related

Get-Content splits path with spaces in powershell

I want to get the content of a simple text file with powershell.
This is my code:
$versionPath = "$PSScriptRoot/../VERSION"
Get-Content $versionPath -Raw
I get this error:
C:\Users\Rik : The term 'C:\Users\Rik' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\Users\Rik van\source\repos\
+ FullyQualifiedErrorId : CommandNotFoundException
Get-Content splits the path by spaces, I tried the suggestions here
but nothing seems to work. any suggestions?
My spidey-sense says you're calling your script on the command line like this:
C:\> powershell C:\Users\Rik van\source\repos\myscript.ps1
The problem isn't the content of your script - it's the path to it.
PowerShell is treating the space as the end of the path to the script, and assumes the rest of the filename is parameters to pass to it. The only problem is it can't find a PowerShell script called C:\Users\Rik.
If you wrap the filename in quotes it'll probably just work:
C:\> powershell "C:\Users\Rik van\source\repos\myscript.ps1"

Powershell spaces in script path

I am trying to streamline how I execute some scripts I wrote by setting up a function and alias to run them. I currently have functions to change my directory to where the scripts need to be run, but when I try to run the script itself I get the following error:
C:\Users\me\Desktop\BoB : The term 'C:\Users\me\Desktop\BoB' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ C:\Users\me\Desktop\BoB Tools\folderScannerV0.4.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\jteit\Desktop\BoB:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The function is:
function run-scanner { "& 'C:\Users\me\Desktop\BoB Tools\folderScannerV0.4.ps1'" | Invoke-Expression }
I've tried a few variations based on other answers I've found, but I keep getting the same error. I would prefer to not remove the space on the path because other scripts use it successfully. Running the script from the ISE gives me no problems.
Ideally I would like the function to also allow me to have the script run on the folders I would like without changing the working directory (each script works on a particular set of files that are in a static location but some of them use $PWD to get the folders in the location).
For example in my $profile file I have this function: function go-to-temp {cd "C:\Users\me\Desktop\Bob Tools\To be Formatted\Temp"} which I run before I execute the above script. I would like them rolled into a single command without my working directory changing (which would render the go-to-temp function redundant.
What am I doing wrong?
There is no reason to run your script through Invoke-Expression.
Unless your script relies on $PWD, then you should be able to execute it with the call operator: &. As the other poster mentioned, you can use dot-sourcing (.) if you need the variables the script generates, but this will import all global objects (aliases, variables, functions) to your current scope. If it does rely on $PWD, you can utilize Start-Process with -WorkingDirectory to avoid changing where you're at.
function Start-Scanner {
& "$HOME\Desktop\BoB Tools\folderScannerV0.4.ps1"
}
or
function Start-Scanner {
$startArgs = #{
FilePath = "$PSHOME\powershell.exe"
ArgumentList = '-File', "`"$HOME\Desktop\BoB Tools\folderScannerV0.4.ps1`""
WorkingDirectory = "$HOME\Desktop\BoB Tools"
NoNewWindow = $true
Wait = $true
}
Start-Process #startArgs
}
You can just use dot-sourcing for this:
function run-scanner { . 'C:\Users\me\Desktop\BoB Tools\folderScannerV0.4.ps1' }

Error when using New-Item / md / mkdir

I've currently got a script that:
Loops through some files
Checks the name of the file
If there is no directory for the file it will create one
Moves the file into the directory
The moving logic works fine. However if a directory does not exist I am given this error (the path is valid, except it does not exist)
C:\Users\User\Documents\Directory\FileName : The term 'C:\Users\User\Documents\
Directory\FileName' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
C:\Users\User\Documents\Directory\FileName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\User\Documents\Directory\FileName) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The curious part is that it does actually create the folder - however it crashes my script.
Here's the problem part of the script
function CanCreate($dir) {
return !(Test-Path $dir)
}
if (CanCreate($fullDestinationPath)) {
New-Item $fullDestinationPath -ItemType Directory
}
md/mkdir behave differently to New-Item in that they do crash the script, however New-Item prints the error and continues (script seems to finish its job).
Edit:
The issue seems to stem from the fact that I am calling the script from another script.
$ScriptPath = "C:\Powershell Scripts\script.ps1"
& $ScriptPath | Invoke-Expression
The issue was with the fact that I was using | Invoke-Expression to trigger the script.
Simply calling & $ScriptPath, omitting the | Invoke-Expression, was enough to trigger the script.
Thanks everyone.

Open program files directory using powershell and env variable

My goal is to create a PowerShell script, which would open specific directories in Windows Explorer.
Some of the directories could be referenced through the environment-variables.
However I got a problem with the following command
ii %programfiles(x86)%
Execution returns the following error:
The term 'x86\' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:23
+ ii %programfiles\(x86\ <<<< )%
+ CategoryInfo : ObjectNotFound: (x86\:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Can you kindly explain to me, what I'm doing incorrectly here?
%variable% is batch notation. In PowerShell you have to use $env: to access environment variables.
Invoke-Item ${env:ProgramFiles(x86)}
The curly brackets are required, because without them the parentheses wouldn't be recognized as part of the variable name.

Powershell | Unable to perform command with space character

Can anyone explain to me why:
iex "C:\Program Files\test\test.exe"
Returns:
C:\Program : The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\Program Files\test\test.exe
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Program:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I've tried to get this working multiple different ways:
Wrapping the text in ()
Putting the string into a variable and passing it as a variable instead
Using single quotes
Using double quotes
I don't know how else I can get it to realize that the entire string must be run, not just the first word.
Post-answered example
The question has been answered. Here is something I was trying to get working:
$tool = "C:\Windows\System32\cmd.exe"
$param = "/c ping google.com -n 1"
$test = & $tool $param
Write-Host $test
It turns out that the line with & does NOT work with double quotes "" in this instance, and actually worked without them. I think this has to do with there being arguments/parameters involved.
Use the & operator together with quotes:
& "C:\Program Files\test\test.exe"
From help about_operators:
& Call operator
Runs a command, script, or script block. The call operator, also known as
the "invocation operator," lets you run commands that are stored in
variables and represented by strings. Because the call operator does not
parse the command, it cannot interpret command parameters.
C:\PS> $c = "get-executionpolicy"
C:\PS> $c
get-executionpolicy
C:\PS> & $c
AllSigned