Unable to Run PowerShell Scripts on Windows Server 2012 R2 Standard

This topic contains 14 replies, has 5 voices, and was last updated by  Olaf Soyk 4 weeks, 1 day ago.

  • Author
    Posts
  • #102665

    Rajkumar Balasakuri
    Participant

    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.

  • #102667

    Olaf Soyk
    Participant

    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?

    • #102673

      Rajkumar Balasakuri
      Participant

      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.

    • #102676

      Olaf Soyk
      Participant

      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?

  • #102674

    Meldsa
    Participant

    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.

    • #102688

      Rajkumar Balasakuri
      Participant

      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.

  • #102689

    Olaf Soyk
    Participant

    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?

    • #102694

      Rajkumar Balasakuri
      Participant

      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.

    • #102706

      Olaf Soyk
      Participant

      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?

  • #102764

    postanote
    Participant

    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.

    • #102823

      Rajkumar Balasakuri
      Participant

      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.

    • #102833

      Jon
      Participant

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

    • #102838

      Olaf Soyk
      Participant

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

    • #102844

      Jon
      Participant

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

    • #102845

      Olaf Soyk
      Participant

      We have a proverb: "You grow with your tasks" 😉

You must be logged in to reply to this topic.