19 Replies Latest reply on Jan 30, 2018 3:37 PM by deverts

    Passing ${CREDENTIAL} in a PowerShell without using -Credential

    jbiggley

      There is an out-of-the-box template for Groupwise performance metrics that a client is trying to use in their environment.  They use local authentication for the target webpage but, try as I might, I've been unable to figure out how to pass the variable ${credential} into the Net-Object System.Net.WebClient object. (See lines 18-20 below)

       

      I tried inserting the following on line 19 but that fails with 'The user name or password is incorrect.'

      $webClient.Credentials = new-object System.Management.Automation.PSCredential(${CREDENTIAL});

       

      As per the script body of Windows PowerShell monitor the ${CREDENTIAL} variable will pass the username and the component will prompt for the password at run time, which SAM will then pass from the SAM credential store.

       

      I got the option for line 19 (as above) from How to make an authenticated web request in Powershell? - Stack Overflow.

       

      Tagging mrxinu on this one for some help!

       

      $Error.Clear();
      $server = $args[0];
      $port = $args[1];
      if ( !$server )
       {
       Write-Host "Message: Can't find ""server"" argument. Check documentation.";
       exit 1;
       }
      if ( !$port )
       {
       Write-Host "Message: Can't find ""port"" argument. Check documentation.";
       exit 1;
       }
      $tempvar = (Get-Childitem env:temp).value;
      $tempfile = $tempvar + '\APM-GW-POA1' + $server + '.txt';
      $downloadstring = 'http://' + $server + ':' + $port + '/';
      try {
        $webClient = New-Object System.Net.WebClient;
        
        $content = $webClient.DownloadString("$downloadstring") | out-file $tempfile;
        }
      catch {
        Write-Host "Message: Connection error: $($Error[0])";
        exit 1;
        }
      function getstat2([string] $path, [string] $pattern) {
       $i = (get-content $path | select-string $pattern | Select-Object LineNumber | select -first 1).LineNumber;
       if ($i -eq $null)
        {
        return -1;
        }
       else
        {
        $out = get-content $path;
        $i=$i+1;
        $t=$out[$i].Split("<*>");
        return $t[2];
        }
       }
      function getstat1([string] $path, [string] $pattern) {
       $i = (get-content $path | select-string $pattern | Select-Object LineNumber | select -first 1).LineNumber;
       if ($i -eq $null)
        {
        return -1;
        }
       else
        {
        $out = get-content $path;
        $t=$out[$i].Split("<*>");
        return $t[4];
        }
       }
      $stat1 = getstat1 $tempfile "C/S Users";
      $stat2 = getstat2 $tempfile "Application Connections";
      $stat3 = getstat2 $tempfile "Physical Connections";
      $stat4 = getstat2 $tempfile "Priority Queues";
      $stat5 = getstat2 $tempfile "Normal Queues";
      $stat6 = getstat1 $tempfile "C/S Requests";
      $stat7 = getstat1 $tempfile "C/S Requests Pending";
      $stat8 = getstat1 $tempfile "Users Timed Out";
      $stat9 = getstat2 $tempfile "Rules Executed";
      Write-Host "Statistic.Users: $stat1";
      Write-Host "Statistic.Application_Connections: $stat2";
      Write-Host "Statistic.Physical_Connections: $stat3";
      Write-Host "Statistic.Priority_Queues: $stat4";
      Write-Host "Statistic.Normal_Queues: $stat5";
      Write-Host "Statistic.Requests: $stat6";
      Write-Host "Statistic.Requests_Pending: $stat7";
      Write-Host "Statistic.Users_Timed_Out: $stat8";
      Write-Host "Statistic.Rules_Executed: $stat9";
      exit 0;