INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Create Powershell Script from Invoke-Command

Create Powershell Script from Invoke-Command

(OP)
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.

RE: Create Powershell Script from Invoke-Command

(OP)
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.

RE: Create Powershell Script from Invoke-Command

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 --> PowerShell

# 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 http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

RE: Create Powershell Script from Invoke-Command

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close