1

I am trying to make a script that will prompt the user for their computer name, then use that info in a Get-EventLog line. I am new to powershell and I use to do it with batch scripts however I am not sure how to do it here.

This is what I have so far:

$name1 = Read-Host 'Enter name'
cls
Get-EventLog system -computername COMPUTERNAMEHERE -InstanceId 2147489657 -Newest 5 | ft EventID,TimeWritten,MachineName -AutoSize
Pause

Also... how do would I make this run by double clicking on it like you would with a batch script?

Aaron
  • 3,135
  • 20
  • 51
  • 78
  • 3
    Maybe I'm misunderstanding something...are you just asking how to use the value you read into **$name1** as the argument to the **-ComputerName** paramater? Just replace `COMPUTERNAMEHERE` with the variable, **$name1**. – Adi Inbar Nov 22 '13 at 00:10
  • See this [post](http://stackoverflow.com/questions/10137146/is-there-any-way-to-make-powershell-script-work-by-double-clicking-ps1-file) about double-clicking and powershell. – David Nov 22 '13 at 00:22
  • That's all you do? Now I feel dumb. – Aaron Nov 22 '13 at 00:32
  • It runs but if I try to run it with the shortcut I get an error: cannot be loaded because running scripts is disabled on this system. Do you know what I can add in the script to run this without turning off restriction completely? Just for this script. – Aaron Nov 22 '13 at 00:57
  • @Aaron You configure your Execution Policy. See [`Get-ExecutionPolicy`](http://technet.microsoft.com/en-us/library/hh849821.aspx) and [`Set-ExecutionPolicy`](http://technet.microsoft.com/en-us/library/hh849812.aspx). – jscott Nov 22 '13 at 01:22
  • Yes I saw that but is there a way to only run it for that script while having it Restricted at any other time? – Aaron Nov 22 '13 at 01:34
  • 1
    You can also call the PowerShell interpreter directly, telling it to use Bypass for the current file only: `powershell.exe -ExecutionPolicy Bypass -File ` – jscott Nov 22 '13 at 01:53
  • I created a shortcut with that and it works fine ty. – Aaron Nov 22 '13 at 02:03

1 Answers1

1

Not really answering your first question, as you seem to have that covered, Regarding the error that you get about disabled scripts: By default and design, Microsoft closed a major attack vector that was present in all it's previous scripting attempts: the arbitrary execution of code. You will need to look into Execution Policies to learn more, but basically, out of the box, PowerShell only allows line by line execution from a command host.

@jscott is right that the -ExecutionPolicy Bypass parameter to the PowerShell exe will work for you, but I would recommend you understand the implications of the Bypass execution policy if you use this all over the place.

My favorite policy for trusted machines is the RemoteSigned policy. It gives you a measure of security against arbitrary execution (any script not originating on your machine needs to be cryptographically signed by trusted certificate) and still allows you to run scripts.

Joseph Alcorn
  • 2,322
  • 17
  • 23
  • But there is something I can run just for that script that will allow it work while it runs and thats it? – Aaron Nov 22 '13 at 13:08