how to put a powershell script inside a batch file

Member_2_6474242
Member_2_6474242 used Ask the Experts™
on
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."
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
MichelangeloSystem Administrator / Postmaster

Commented:
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
Michael B. SmithManaging Consultant

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

if not, this is exactly the error I would expect.
Top Expert 2016

Commented:
@@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/
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Member_2_6474242Systems Administrator

Author

Commented:
Hi David
I tried the code and i got the error attachedsmb.txt
Member_2_6474242Systems Administrator

Author

Commented:
Hi Michael

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

Where exactly do i need to copy this to?
Top Expert 2016

Commented:
you need to run the batch file as an administrator
Member_2_6474242Systems Administrator

Author

Commented:
Hi Michelangelo

I tried your code and i got the error attached
smb.txt
Member_2_6474242Systems Administrator

Author

Commented:
Hi David

How can i accomplish that if i put it on logon script for a user in AD
Top Expert 2016

Commented:
don't put it in a user logon script but a computer logon script where it will run as system.
Member_2_6474242Systems Administrator

Author

Commented:
HI David just tried that. the group policy is updated but the feature is not installed
Member_2_6474242Systems Administrator

Author

Commented:
thanks David it seemed like it worked. I didnt have the computer name in the same ou that i was applying it to
Top Expert 2016

Commented:
you have to shutdown and restart one more time
Member_2_6474242Systems Administrator

Author

Commented:
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.
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Aside of the scripting issues: you are aware that you are compromising the whole AD by enabling SMBv1, not only making your network less secure but really weak? It doesn't matter how much it is used, just having it used is bad.
Of course there are NAS and other devices still unable to speak SMBv2.

Back to scripting, I would split the feature install to a computer startup script with forced reboot if required, and then use the in-built GPO features to map drives (or your script) assigned to login.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial