Unable to Run PowerShell Scripts on Windows Server 2012 R2 Standard

Problem :- Unable to Run PowerShell Scripts on Windows Server 2012 R2 Standard

1 . Right Click on .ps1 file and Run with powershell.

Result
a. Nothing happens . Power Shell Screen Flashes away and exits.
b. Get-ExecutionPOlicy cmdlet always returns Unrestricted.

Can you all guide me if any settings\IT policy requirement that needs to be checked.
An help would be highly appreciated on this.

Did you try to open a Powershell console window and run the script from there by typing or droping it’s name including the complete path?

Hi Olaf,

Thanks for quick reply.

It works

PS C:\Users\rbalasakuri> D:\Rooster\Scripts\MachinesInfo.ps1. ( this works)

but my original problem doesnot work. Can you please let me know what could be a problem.

I’m Pretty sure thats because it closes down the session after your script is run.

I would suggest to create a dummy script that just creates a file and see if that file gets created after Right-Click - Run with PowerShell.

PS C:\Users\rbalasakuri> D:\Rooster\Scripts\MachinesInfo.ps1. ( this works)
Is it that what you try to run with a right click? If not - please run the script you're talking about from the prompt in an open Powershell console window and see what happens. Did you write the script?

Here is my simple script ( like this there are other scripts which i need to run daily for monitoring purposes)

$Machines = Get-Content -Path D:\Rooster\Scripts\ProjectMachinesNames.txt (This file exists at the mentioned Place)
foreach ($Computer in $Machines)
{
Get-Volume -CimSession $Computer.Trim()
}
PowerShell.exe

Previously the above script used to output the results onto power shell screen but from couple of days on am facing this kind problem.

OK, I will ask only once more: Did you run this script in an open Powershell console window? What happened? Didn’t you get any errors?

Sorry if could not reply for your question properly (if any)

Yes the script runs perfectly fine and it gives results on the power shell Console Window and i didn’t get any errors.

Yes the script runs perfectly fine and it gives results on the power shell Console Window and i didn't get any errors.
Sorry, but what's the problem then?

Are you saying you are not running this…

$Machines = Get-Content -Path D:\Rooster\Scripts\ProjectMachinesNames.txt (This file exists at the mentioned Place)
 foreach ($Computer in $Machines)
 {
 Get-Volume -CimSession $Computer.Trim()
 }

… from the console or ISE environment directly these days, and thus trying to do this…

$Machines = Get-Content -Path D:\Rooster\Scripts\ProjectMachinesNames.txt (This file exists at the mentioned Place)
 foreach ($Computer in $Machines)
 {
 Get-Volume -CimSession $Computer.Trim()
 }
 PowerShell.exe 

… say from cmd.exe or other console?

If so that will never work.

If you are in cmd.exe then you need to approach this differently.
Save this as a script file…

$Machines = Get-Content -Path D:\Rooster\Scripts\ProjectMachinesNames.txt (This file exists at the mentioned Place)
 foreach ($Computer in $Machines)
 {
 Get-Volume -CimSession $Computer.Trim()
 }

Then in using cmd.exe do…

PowerShell WhateverYourScriptPathIs\YourScriptName.ps1
powershell d:\Temp\Test.ps1

If you are in the console or ISE console or script pane, just run your script

WhateverYourScriptPathIs\YourScriptName.ps1

So, if your code is running fine from the console host, ISE and or VSCode, then there is nothing wrong with your code.
If you are not using the above, then you are in the correct execution environment to run your code.

The Idea is to Right Click on the file MachinesInfo.ps1 and execute (Run with powershell) and here am failing to see the output of the script on Powers hell Console.

If you want to see the results of a script you have to run the script with the console already open.

… or you have to put something to the end of your script what keeps the console window open.

I figured that was going to be too much of a reach for this :wink:

We have a proverb: “You grow with your tasks” :wink: