1 Reply Latest reply on Apr 15, 2015 5:12 AM by Reinhard Vielhaber

    Powershell through NSH

    Don Kim

      I am trying to run a one-liner PS script through NSH but seem to be getting stdin errors. Here is what I am trying to do:


      nexec -e cmd /c \"powershell -Command &{$file='c:\temp\test.txt';$process=[diagnostics.process]::Start('secedit.exe','/export /areas USER_RIGHTS /cfg $file');$in=get-content $file;$export='';$nl=[Environment]::NewLine;foreach ($line in $in) {$line;if($line -match '\=' -AND $line -match '\*') {$sids = $line.split('=');$sidcount=$sids[1].split(',');if($sidcount.count -gt 1) {$sidTrans = '';foreach($i in $sidcount) {$value = $i.replace('*','');$value=$value.replace(' ','');$objSID = New-Object System.Security.Principal.SecurityIdentifier($value);$objUser = $objSID.Translate( [System.Security.Principal.NTAccount]);if($objUser.Value -ne '') {$export += $sids[0] + ' = ' + $objUser.Value + $nl};}}else{$sidTrans=''; $value=$i.replace('*','');$value = $value.replace(' ','');$objSID=New-Object System.Security.Principal.SecurityIdentifier($value); $objUser=$objSID.Translate( [System.Security.Principal.NTAccount]);$sidTrans=$objUser.Value;$export+=$sids[0]+' = '+$sidTrans+$nl}}else{$export += $line + $nl}}cls;write-host $export}\"

      Also tried nexec -i -ncq as well. Error starting ($line in $in). Has anyone had success in something like this?

        • 1. Re: Powershell through NSH
          Reinhard Vielhaber

          According to some other posts and my own investigation (which is not finished ;-) ) you could run it as type-2-script with the following commandline-parameters between "powershell" and "-command":


          -executionpolicy bypass -inputformat none -noninteractive


          There are postings here in the community mentioning more parameters. But I found that the additional ones are - at least for my very simple PS-script - not necessary.