Running Script Within a Script

Welcome Forums General PowerShell Q&A Running Script Within a Script

Viewing 5 reply threads
  • Author
    Posts
    • #248644
      Participant
      Topics: 2
      Replies: 3
      Points: 16
      Rank: Member

      We have a script that grants admin rights to our PC’s.  I tend to run it on it’s own to grant myself rights to one or more machines.  Then, run another script that I require.

      To save myself clicks, I’m trying to call the admin script within the currently running script to save time.

      If I try to execute the script via ps command line, the “AdminRights.ps1” does not run at all.

      Seems like the only way it works is when I create and run via a shortcut.  Only odd behaviour is that the “AdminRights.ps1” script seems to run twice for some reason.  First at the start of the script (as intended).  And then again when the actual script runs.  Not sure how to get it to run once and then just move to the next part…

      Here is the example script:

    • #248665
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      We have a script that grants admin rights to our PC’s.

      Just to at least mention it once …. there are GPOs designed to do exactly this … manage the local admin group … you don’t need a script for that … you know that, right? 😉 😀

    • #248785
      Participant
      Topics: 2
      Replies: 3
      Points: 16
      Rank: Member

      yup! definitely aware :).  Without getting into too many details.  We’re not allowing the GPO’s for this set of machines, in our environment….  And the admin script we have is specific to the machines in the env we’re running them on etc etc…

    • #249140
      Senior Moderator
      Topics: 9
      Replies: 1310
      Points: 4,789
      Helping Hand
      Rank: Community Hero

      You don’t need the dot sourcing model to run the script, and from the error it looks like the adminrights.ps1 is not available from the location you are triggering the parent script. You can better use absolute path instead of relative path. You don’t need wrap the script in quotes as well.

    • #249164
      Participant
      Topics: 7
      Replies: 123
      Points: 599
      Helping Hand
      Rank: Major Contributor

      Just my $.02 on Get-ComputerInfo .. I have found it can take a fair amount of time if the list of computers is a long list. Using Get-WmiObject and the Win32_OperatingSystem and Win32_ComputerSystem classes is much faster.

    • #250508
      Participant
      Topics: 2
      Replies: 3
      Points: 16
      Rank: Member

      You don’t need the dot sourcing model to run the script, and from the error it looks like the adminrights.ps1 is not available from the location you are triggering the parent script. You can better use absolute path instead of relative path. You don’t need wrap the script in quotes as well.

      Seems like what solved my issue was placing the full path, instead of just ‘.\adminrights.ps1’.  Which is weird, because the ‘.\adminrights.ps1’ is sitting in the same folder as the main script….  But I’ll take the win.

      Even though this works, the behaviour is that the admin script will run twice…  Once when initially prompted by the admin script.  Then it grants admin access again, when main script is running.  Any ideas why?

       

      Just my $.02 on Get-ComputerInfo .. I have found it can take a fair amount of time if the list of computers is a long list. Using Get-WmiObject and the Win32_OperatingSystem and Win32_ComputerSystem classes is much faster.

      Hi.  I agree with you that your method makes more sense.  Especially if using for multiple computers.  But, when creating it, my goal was to ultimately get the OS version number (ie – 1709, 1909, 2004).  “Get-ComputerInfo gave me the most direct method to do this, so I just went with it.

      Do you have any proper suggestions for getting the OS version and not Build Number?

Viewing 5 reply threads
  • You must be logged in to reply to this topic.