3

I have a batch file that ask the user for a variable line set /p asset=. Im calling my powershell script like this

SET ThisScriptsDirectory=%~dp0

SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

Im wondering how i send powershell the variable 'asset' from my batch file.

Here is my .bat file content

@Echo off  
cls
Color E
cls

@echo Enter asset below
set /p asset=

@echo.
@echo your asset is %asset%
@echo.
goto startusmt

:startusmt
@echo.
@echo executing psexec ...
@echo.

SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%RemoteUSMT.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -file %PowerShellScriptPath% %asset%
psexec \\%asset% -u domain\username -p password cmd 

goto EOF

:EOF 
PAUSE
rangerr
  • 261
  • 2
  • 5
  • 13

3 Answers3

5

You can use $env:variable_name syntax to access curent cmd environment variables from powershell. To get hold of your variable you'd use $env:asset
To try, open cmd, do set "myvar=my value", start powershell, do $env:myvar (this will simply print it, but of course you can use it as any other ps variable)

Just as a sidenote, ps has good help system. If you do help env it will list two relevant topics which you can examine in turn to get detailed information.

Hope this helps

wmz
  • 3,645
  • 1
  • 14
  • 22
  • Yes this is an option, but generally it's better to have arguments that you pass in to the PS script so that you can tell by the function signature what the script depends on for input. It also makes the PowerShell script more usable if calling directly from PowerShell. – Rynant Apr 18 '14 at 18:33
  • @Rynant Generally yes, but sometime somewhere you may find you have control over the script but not it's invocation/environment and then it comes in handy :-) – wmz Apr 18 '14 at 18:50
4

If you have a file.ps1 that takes a parameter,

param($asset)
"The asset tag is $asset"

You can pass in the variable as an argument.

SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps1
SET /p asset=Enter the asset name

PowerShell -NoProfile -ExecutionPolicy Bypass -file "%PowerShellScriptPath%" "%asset%"
Rynant
  • 23,153
  • 5
  • 57
  • 71
  • File runs the PowerShell script. You can use `-file` in place of calling the file from `-command`. – Rynant Apr 18 '14 at 16:09
  • `PowerShell -NoProfile -ExecutionPolicy Bypass -file "%PowerShellScriptPath%" %asset%` Like this? – rangerr Apr 18 '14 at 16:22
  • How do I reference that variable in powershell? – rangerr Apr 18 '14 at 16:28
  • In the example that I gave, the value of `%asset%` is passed to the `$FileName` variable. You can use whatever variable name you want; the point is that the arguments listed in the batch file get passed to the param variables in the .ps1 script. If your .ps1 took two parameters (e.g. `param($one, $two); write-host "one: $one, two: $two"`) you could call it from a batch file with `PowerShell -file %PowerShellScriptPath% %assetA% %assetB%` – Rynant Apr 18 '14 at 16:59
  • This is what I have right now and the powershell line wont display my variable asset when I try to call it. bath file line is `PowerShell -NoProfile -ExecutionPolicy Bypass -file %PowerShellScriptPath% %asset%` Then in my .ps file I have `param($asset)` `Get-Item $asset` `"The asset tag is $asset"` . But the script never puts the asset I typed in the .bat file – rangerr Apr 18 '14 at 18:21
  • @rangerr You don't want `Get-Item $asset` in your PowerShell script. I only used it as an example in my script. – Rynant Apr 18 '14 at 18:23
  • even with the `Get-Item $asset` left out im still not able to have the $asset in powershell to to the %asset% I defined in the .bat file. – rangerr Apr 18 '14 at 18:31
  • Have you tried the example exactly as it is in my answer? It works for me. Maybe you are doing something differently in your bat file. – Rynant Apr 18 '14 at 18:43
  • my .ps file only has your 2 lines plus `"The asset tag is $asset`. my .bat file looks like my first post but with your edit to the powershell line – rangerr Apr 18 '14 at 18:45
  • The PowerShell script should be .ps1 not .ps – Rynant Apr 18 '14 at 18:48
  • Sorry yes its called "RemoteUSMT.ps1" and in side its `param($asset)` and `"The asset tag is $asset` – rangerr Apr 18 '14 at 18:50
  • Hmm, that batch file works for me; I see 'the asset tag is '. Are you seeing PowerShell only outputting: "the asset tag is "? – Rynant Apr 18 '14 at 19:06
  • Yes its only saying "the asset tag is " and not inputting the $asset variable – rangerr Apr 18 '14 at 19:10
  • I'll post an answer below with my batch file content if that helps – rangerr Apr 18 '14 at 19:15
  • I tried the .bat text from your updated post and it worked for me, so I'm stumped as to why it's not working. You may want to put quotes around both variables on the powershell call in the batch file in case there is a space in the path or the user's input, but I don't think that is the problem if powershell outputs something. Have you tried @wmz`s answer? It would be interesting to hear if that works. Just make `"The asset tag is " + $env:asset` the only line in your .ps1 file. – Rynant Apr 18 '14 at 19:36
  • That work. Im not sure why that way works and the other way doesnt but at least it works now. Thank you for helping me figure this out! – rangerr Apr 21 '14 at 12:47
  • @Rynant What if I'm calling powershell like this: `powershell -Command "Remove-Item %filePath%\* -Recurse -Exclude '*.avi','*.mkv','*.mp4'"`? I.e. not using a script, but simply invoking a single command? This doesn't seem to work for me :-/ I've also tried wrapping the command in braces like so `&{command}`, separating the command into its own batch variable, using `setlocal` and all sorts of weird hacks, but no dice. This seems to be such a simple thing to do but really isn't for a batch and powershell n00b like me! – Kenny83 Nov 01 '19 at 03:17
0
param($asset)

This has to be the very first line in the PowerShell script for it to work, else it will fail.

Andreas
  • 5,393
  • 9
  • 44
  • 53
Srid
  • 1
  • 1