Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Powershell Script from Invoke-Command 1

Status
Not open for further replies.

SteveR77

Programmer
Sep 18, 2000
813
US
I am new to PowerShell but I have been able to work with DNS entries using the following command line commands but when I attempt to place them in a PowerShell script I get a File Path error.

Would someone enlighten me as to where I am going wrong? This is a small part of a larger project with the end goal being the PowerShell script is called from a Linux bash script and passed the variables it needs for the PowerShell DSN script.

Thank you for your assistance.

$command = { dnscmd mcaddnstdc01 /RecordDelete mclane.mclaneco.com vcacdnstest A 10.40.27.117 /f }
Invoke-Command -ComputerName mcaddnstdc01 $command

$command = { dnscmd mcaddnstdc01 /RecordAdd mclaneco.com vcacdnstest A 10.40.27.117 }
Invoke-Command -ComputerName mcaddnstdc01 $command




I.T. where a world of choas and confusion comes down to nothing but 1s and 0s.
 
D'OH!!! I have this figured out. I failed to run PowerShell as an admin.



I.T. where a world of choas and confusion comes down to nothing but 1s and 0s.
 
here is a way to not worry about that. Just put this code at the top of your script. It will ensure the script is running as admin, if not it closes and relaunches itself as admin.

Code:
 # Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
 
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
 
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
   {
   # We are running "as Administrator" - so change the title and background color to indicate this
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
   }
else
   {
   # We are not running "as Administrator" - so relaunch as administrator
   
   # Create a new process object that starts PowerShell
   $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
   
   # Specify the current script path and name as a parameter
   $newProcess.Arguments = $myInvocation.MyCommand.Definition;
   
   # Indicate that the process should be elevated
   $newProcess.Verb = "runas";
   
   # Start the new process
   [System.Diagnostics.Process]::Start($newProcess);
   
   # Exit from the current, unelevated, process
   exit
   }
 
# Run your code that needs to be elevated here

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Thanks again Mark. PowerShell is not my normal coding playground and I am trying to get up to speed quickly in an environment that has both PS 3.0 and PS 4.0.

I.T. where a world of choas and confusion comes down to nothing but 1s and 0s.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top