Link to home
Start Free TrialLog in
Avatar of Member_2_6474242
Member_2_6474242Flag for Fiji

asked on

how to put a powershell script inside a batch file

Hi All

I am trying to include the following into a batch file that i have that maps some drives

i have added the below to my smb.ps1
Enable-WindowsOptionalFeature -Online -FeatureName smb1protocol

In my batch file
powershell.exe -ExecutionPolicy Bypass -File "D:\scripts\smb.ps1"

Also note the smb script works if i test it alone

I get the error below when i execute. i have allowed allow local and remote signed scripts on dc and this is happening via GPO

"The argument 'D:\scripts\smb.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter."
Avatar of Michelangelo
Michelangelo
Flag of Italy image

Try this

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""D:\scripts\smb.ps1""' -Verb RunAs}"

And run as administrator
did you copy "D:\Scripts\smb.ps1" to every computer?

if not, this is exactly the error I would expect.
@@ECHO off
@@setlocal EnableDelayedExpansion
@@set LF=^


@@SET command=#
@@FOR /F "tokens=*" %%i in ('findstr -bv @@ "%~f0"') DO SET command=!command!!LF!%%i
@@powershell -noprofile -noexit -command !command! & goto:eof


# *** POWERSHELL CODE STARTS HERE *** #
# put the contents of the ps1 here
#

Open in new window

https://blogs.technet.microsoft.com/pstips/2018/01/25/embed-powershell-code-in-a-batch-file/
Avatar of Member_2_6474242

ASKER

Hi David
I tried the code and i got the error attachedsmb.txt
Hi Michael

did you copy "D:\Scripts\smb.ps1" to every computer? No

Where exactly do i need to copy this to?
you need to run the batch file as an administrator
Hi Michelangelo

I tried your code and i got the error attached
smb.txt
Hi David

How can i accomplish that if i put it on logon script for a user in AD
don't put it in a user logon script but a computer logon script where it will run as system.
HI David just tried that. the group policy is updated but the feature is not installed
thanks David it seemed like it worked. I didnt have the computer name in the same ou that i was applying it to
you have to shutdown and restart one more time
ok it didnt seem to install it
Mapping drives from the batch file, then running powershell from within it seems convoluted.  Why not just put the entire thing in Powershell?  You can map drives Via GPO too.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial