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?

8
  • 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. Commented Nov 22, 2013 at 0:10
  • See this post about double-clicking and powershell. Commented Nov 22, 2013 at 0:22
  • That's all you do? Now I feel dumb. Commented Nov 22, 2013 at 0: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. Commented Nov 22, 2013 at 0:57
  • 1
    You can also call the PowerShell interpreter directly, telling it to use Bypass for the current file only: powershell.exe -ExecutionPolicy Bypass -File <script_name> Commented Nov 22, 2013 at 1:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But there is something I can run just for that script that will allow it work while it runs and thats it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.