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."
Member_2_6474242Senior Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MichelangeloConsultantCommented:
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
0
Michael B. SmithManaging ConsultantCommented:
did you copy "D:\Scripts\smb.ps1" to every computer?

if not, this is exactly the error I would expect.
0
David Johnson, CD, MVPOwnerCommented:
@@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/
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Member_2_6474242Senior Systems AdministratorAuthor Commented:
Hi David
I tried the code and i got the error attachedsmb.txt
0
Member_2_6474242Senior Systems AdministratorAuthor Commented:
Hi Michael

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

Where exactly do i need to copy this to?
0
David Johnson, CD, MVPOwnerCommented:
you need to run the batch file as an administrator
0
Member_2_6474242Senior Systems AdministratorAuthor Commented:
Hi Michelangelo

I tried your code and i got the error attached
smb.txt
0
Member_2_6474242Senior Systems AdministratorAuthor Commented:
Hi David

How can i accomplish that if i put it on logon script for a user in AD
0
David Johnson, CD, MVPOwnerCommented:
don't put it in a user logon script but a computer logon script where it will run as system.
0
Member_2_6474242Senior Systems AdministratorAuthor Commented:
HI David just tried that. the group policy is updated but the feature is not installed
0
Member_2_6474242Senior Systems AdministratorAuthor Commented:
thanks David it seemed like it worked. I didnt have the computer name in the same ou that i was applying it to
0
David Johnson, CD, MVPOwnerCommented:
you have to shutdown and restart one more time
0
Member_2_6474242Senior Systems AdministratorAuthor Commented:
ok it didnt seem to install it
0
serialbandCommented:
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.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.