Return Value of Powershell with Batch file


i want verify ad credentials given in batch file. 

i got powershell script 2.ps1

function test-adcredentials {
      param([string]$username,[string]$pwd,[string]$domain)
      add-type -assemblyname system.directoryservices.accountmanagement
      $ct = [system.directoryservices.accountmanagement.contexttype]::domain
      $pc = new-object system.directoryservices.accountmanagement.principalcontext($ct, $domain)
      new-object psobject -property @{
           username = $username;
           isvalid = $pc.validatecredentials($username, $pwd).tostring()
      }
}

and tried call ps1 file batch file.

@echo off
set r_machine=xxx
set password=*******
set r_user=administrator
powershell.exe -executionpolicy bypass .\2.ps1 -username %r_user% -pwd %password% -domain %r_machine%

if credentials true, batch file should perform further actions.

else batch file should quit.

i want capture return value ps1 , use in bat file..

please 1 me..

and sorry english..!



make easier on , output result in ps1 script file, , don't have function in script unless script calls function:

param([string]$username,[string]$pwd,[string]$domain) add-type -assemblyname system.directoryservices.accountmanagement $ct = [system.directoryservices.accountmanagement.contexttype]::domain $pc = new-object system.directoryservices.accountmanagement.principalcontext($ct, $domain) $pc.validatecredentials($username, $pwd).tostring() 

you don't need return username since passed username script anyway.  then call script, capture output, , evaluate this:

for /f %%i in ('powershell .\2.ps1 -username %r_user% -pwd %password%') set result=%%i if %result%==true <do something>
tested @ console %i, in batch file need %%i.


i hope post has helped!



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Edit Group Policy

Hyper-V VM not reaching OS 'Logon' screen

DNS question...