Solved

"Bootstrapping" powershell scripts

Posted on 2016-10-04
8
56 Views
Last Modified: 2016-10-08
I'm trying to set up all the necessary WMI settings for monitoring a workstation by running a local powershell script.
It's going pretty well.  I'm at the stage where it's about ready for production.

Remaining to solve:
I have all the files on a file share and can copy and import modules from there.  
But the workstations aren't set to run scripts.  So I need to first set them up for "unrestricted" as none of my scripts are signed.
(Then I can set them back to "undefined" when I'm finished).
Ideally, I think I'd want to do everything from a single command or desktop shortcut.
(This must be a fairly typical objective).

So how might you suggest I do that?
Right now I have to run some powershell commands to get things started.
While *I* can do that, it's more tedious and not so "exportable" to others.

For example:
Set-ExecutionPolicy Unrestricted
         (and respond "A")
Copy "\\server\c\\Scripts\MyScript.ps1" "$($Env:Temp)"
Import-Module "$($Env:Temp)\MyScript.ps1"

How to fire off all 3 commands without having to type them all and give a response as well?
A simple one-liner would be great!
0
Comment
Question by:Fred Marshall
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41828377
You can temporarily bypass the execution policy to run a script.
Example..
Powershell.exe -ExecutionPolicy Bypass -Command C:\Users\Username\Desktop\Test.ps1

Open in new window

1
 
LVL 25

Author Comment

by:Fred Marshall
ID: 41828702
and if the .ps1 file is on a server?
I tried that and it didn't like it. A folder in the server path has spaces in its name and the error seems related to that.  I tried using double quotes around the string and around the foldername and neither helped.  Sorry I'm so new at this...

I do presume that the example you gave would be run from within PowerShell as that seems to almost work.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41828713
You can run the line from a batch file, a link, PowerShell or whatever.

I would use
Powershell.exe -ExecutionPolicy Bypass -File 'C:\Users\User name\Desktop\Test.ps1'

Open in new window

which works with spaces.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41828718
For using -Command you would have to write
-Command '& "C:\Users\User name\Desktop\Test.ps1" '

Open in new window

1
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 40

Expert Comment

by:Subsun
ID: 41828751
As Qlemo mentioned -File should work

-Command should be..
-Command "& 'C:\Users\User name\Desktop\Test.ps1'"

Open in new window

1
 
LVL 25

Author Comment

by:Fred Marshall
ID: 41828763
I tried this on the local computer:
powershell.exe -ExecutionPolicy Bypass -Command C:\Start_WMI.ps1

Open in new window

and this worked.

Then I tried it back on the server like this:
powershell.exe -ExecutionPolicy Bypass -Command  '&"\\10.0.1.194\c\0 AA Scanner\Scripts\Start_WMI.ps1"'

Open in new window

This one still gets stuck with this error:
& : The term '\\10.0.1.194\c\0' 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:2
+ &\\10.109.1.194\c\0 AA Scanner\Scripts\Start_WMI.ps1
+  ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\10.0.1.194\c\0:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Open in new window

So, it seems that the [zero] [space] remains an issue.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 300 total points
ID: 41828772
powershell.exe -ExecutionPolicy Bypass -Command  "& '\\10.0.1.194\c\0 AA Scanner\Scripts\Start_WMI.ps1'"

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 200 total points
ID: 41829575
Subsun is correct, the double quotes need to be outermost, and the ticks inside, Nevertheless. you should use the -File syntax.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now