You are not logged in.

#1 05 Feb 2016 08:19

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Run CMD as powershell elevation error

I'm using a cmd-powershell hybrid as follows:

<# :
@echo off
setlocal
set "POWERSHELL_BAT_ARGS=%*"
if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF
#>




$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" | where{$_.Index -eq "0"}
foreach($objItem in $colItems) 
{
  Write-Host "Computer Name:  " $env:COMPUTERNAME
	Write-Host "IP Address:     " $objItem.IPAddress[0]
	Write-Host "MAC Address:    " $objItem.MACAddress
  Write-Host "Domain Address: " (gwmi WIN32_ComputerSystem).Domain
}



[void][System.Console]::ReadKey($true)

The script only works on fixed drives. If I try to run this on a removable media, such as a USB device it fails. And if I try to run it from a removable drive it also asks me for my admin credentials when it does not need them. On the HDD it runs without credentials.

The error is something like: the 'ELEV' is not recognized as a command.

Can anyone shed some light on this? Thank you

Last edited by Eehixohw (05 Feb 2016 08:21)

Offline

#2 05 Feb 2016 09:00

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Run CMD as powershell elevation error

What are you trying to achieve with this script?

What's the purpose of the hybridisation?

What arguments are you running with?

Given that the word "ELEV" doesn't appear in the code, it feels like some piece of the puzzle is missing here -- it's difficult to tell without a copy of the exact error message, but I suspect the error coming from the cmd portion.

Try removing the "@echo off" and paste the full output verbatim from both a "good" and a "bad" run.

Last edited by bluesxman (05 Feb 2016 09:06)


cmd | *sh | ruby | chef

Offline

#3 05 Feb 2016 11:11

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Run CMD as powershell elevation error

What are you trying to achieve with this script?

I have scripts like this. It is easier executing powershell scripts like this rather than through the main powershell window.
This particular script will output the main IP from the machine, hostname, domain and mac.

What arguments are you running with?

None.

Given that the word "ELEV" doesn't appear in the code, it feels like some piece of the puzzle is missing here -- it's difficult to tell without a copy of the exact error message, but I suspect the error coming from the cmd portion.

I cannot give you the exact error because I cannot read it. This only happens when it is executed off the usb stick, and when the error appears it closes itself very quickly.

I tried to run it from cmd and powershell consoles and it worked, referencing as "F:\_configuration\_test.cmd" . But if I double click it on the drive that error appears and closes itself.

Offline

#4 05 Feb 2016 12:01

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Run CMD as powershell elevation error

Alexander Ceed wrote:

And if I try to run it from a removable drive it also asks me for my admin credentials when it does not need them. On the HDD it runs without credentials.

It seems like it's a permissions limitation in the way Windows deals with security, in modern versions.

You will find that when this happens that the current working directory is changed to the windows system32 folder, so any script which does not explicitly set the working directory, will have this unexpected different working directory.

I didn't look hard at your script itself - I just recognised this was happening from your description.

Offline

#5 09 Feb 2016 15:14

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Run CMD as powershell elevation error

So no ideas as to how to make this work? I figured that much about working directory but I just can't find a way to edit it. The problem is in the CMD Header that calls powershell.
Edited POWERSHELL_BAT_ARGS but that didn't do anything as it doesn't accept full commands like cd .  .

Offline

Board footer

Powered by