<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: USER-ID agnet get API by VBscript in Automation/API Discussions</title>
    <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/user-id-agnet-get-api-by-vbscript/m-p/135360#M1331</link>
    <description>&lt;P&gt;How about a powershell script?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To schedule this script as a task you should configure the action properties as follow:&lt;/P&gt;&lt;P&gt;Program/Script: powershell&lt;/P&gt;&lt;P&gt;Add Argument: ./UserID.ps1&lt;/P&gt;&lt;P&gt;Start in: &amp;lt;path where you placed the script&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;##################################################################################
#
#  Script name: UserID.ps1
#
##################################################################################

[string]$global:strFirewallIP = "FirewallsIP"
[string]$global:strFirewallAPI = "FirewallsAPI"
[string]$global:strDomain = "DOMAINNAME"
[string]$global:strVsys = "vsys1"
[string]$global:strTimeout = "120"
[string]$global:strLogLevel = "1" # 0: Errors only, 1: Normal Logging, 2: Verbose logging
[string]$global:strLogPath = "UserID.log"

Function AddLog 
{
	param([string]$strMessage, [string]$strLevel = 0)
	If ([int]$global:strLogLevel -gt $strLevel -1)
		{
			$ct = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
			$logmessage = [string]$ct + " - " + $strMessage
			add-content -Path $global:strLogPath -Value $logmessage -Force
		}
}

Try
{
	$message = "Script Launched"
	AddLog $message 2
	[string]$global:UserName = [Environment]::UserName
	[string]$global:ipaddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
	$message = "Found username " + $global:UserName + " with ip address " + $global:ipaddress
	AddLog $message 2

	[string]$strXMLLine = "&amp;lt;uid-message&amp;gt;&amp;lt;version&amp;gt;1.0&amp;lt;/version&amp;gt;&amp;lt;type&amp;gt;update&amp;lt;/type&amp;gt;&amp;lt;payload&amp;gt;&amp;lt;login&amp;gt;&amp;lt;entry name=""" + $global:strDomain + "\" + $global:UserName + """ ip=""" + $global:ipaddress+ """ timeout=""" + $global:strTimeout + """/&amp;gt;&amp;lt;/login&amp;gt;&amp;lt;/payload&amp;gt;&amp;lt;/uid-message&amp;gt;"
	[string]$strEncodedXMLLine = [uri]::EscapeDataString($strXMLLine)

	$message = "Posting mapping to firewall " + $global:strFirewallIP + ": " + $strXMLLine
	AddLog $message 2
	[string]$url = "https://" + $global:strFirewallIP + "/api/?key=" + $global:strFirewallAPI + "&amp;amp;type=user-id&amp;amp;vsys=" + $global:strVsys + "&amp;amp;cmd=" + $strEncodedXMLLine
	[System.Net.HttpWebRequest]$request = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($url)
	$request.Method = "POST"
	$request.ContentType = "text/xml"
	[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
	$message = "Finished Post."
	AddLog $message 2
	try
	{
		[System.Net.HttpWebResponse]$response = [System.Net.HttpWebResponse]$request.GetResponse()     
		$sr = New-Object System.IO.StreamReader($response.GetResponseStream())       
		[Xml]$xmlResponse = $sr.ReadToEnd()
		if ([string]$xmlResponse.FirstChild.status -eq "error") 
		{
			$message = "Error mapping " + $global:ipaddress + " to user '" + $global:strDomain + "\" + $global:UserName + "' on firewall " + $global:strFirewallIP + ": " + $XmlDocument.FirstChild.result.msg
			AddLog $message
		}
		else
		{
			$message = "Sucessfully mapped " + $global:ipaddress + " to user '" + $global:strDomain + "\" + $global:UserName + "' on firewall " + $global:strFirewallIP
			AddLog $message 1
		}
	}			
	catch [Net.WebException] 
	{
		[System.Net.HttpWebResponse] $resp = [System.Net.HttpWebResponse] $_.Exception.Response  
		$message = "Error:" + [string]$resp
		AddLog $message
	}				
	
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
	[string]$message = $FailedItem + " failed with message " + $ErrorMessage
	AddLog $message
    Break
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Keep in mind! The script should run under the logged in users credentials (otherwise it would detect the computers login name or a scheduled login name).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Furthermore: The user should be able to access the script, which is a potential risk, cause this alse reveals the API key to the firewall.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway: this script should get you started.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can check or the XML request was successful in the log file and on the firewall (run “show user ip-user-mapping ip [ip]”)&lt;/P&gt;</description>
    <pubDate>Mon, 02 Jan 2017 13:05:25 GMT</pubDate>
    <dc:creator>Sjoerd</dc:creator>
    <dc:date>2017-01-02T13:05:25Z</dc:date>
    <item>
      <title>USER-ID agnet get API by VBscript</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/user-id-agnet-get-api-by-vbscript/m-p/120348#M1279</link>
      <description>&lt;P&gt;I have an idea hit my head today&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm thinking to schedule a task (let say each half hour) to run a vbscript in user machines to send IP and username to the userid agent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible and can someone help me with code as I'm not that good in coding&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 21:01:12 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/user-id-agnet-get-api-by-vbscript/m-p/120348#M1279</guid>
      <dc:creator>mikealanni</dc:creator>
      <dc:date>2016-10-20T21:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: USER-ID agnet get API by VBscript</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/user-id-agnet-get-api-by-vbscript/m-p/135360#M1331</link>
      <description>&lt;P&gt;How about a powershell script?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To schedule this script as a task you should configure the action properties as follow:&lt;/P&gt;&lt;P&gt;Program/Script: powershell&lt;/P&gt;&lt;P&gt;Add Argument: ./UserID.ps1&lt;/P&gt;&lt;P&gt;Start in: &amp;lt;path where you placed the script&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;##################################################################################
#
#  Script name: UserID.ps1
#
##################################################################################

[string]$global:strFirewallIP = "FirewallsIP"
[string]$global:strFirewallAPI = "FirewallsAPI"
[string]$global:strDomain = "DOMAINNAME"
[string]$global:strVsys = "vsys1"
[string]$global:strTimeout = "120"
[string]$global:strLogLevel = "1" # 0: Errors only, 1: Normal Logging, 2: Verbose logging
[string]$global:strLogPath = "UserID.log"

Function AddLog 
{
	param([string]$strMessage, [string]$strLevel = 0)
	If ([int]$global:strLogLevel -gt $strLevel -1)
		{
			$ct = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
			$logmessage = [string]$ct + " - " + $strMessage
			add-content -Path $global:strLogPath -Value $logmessage -Force
		}
}

Try
{
	$message = "Script Launched"
	AddLog $message 2
	[string]$global:UserName = [Environment]::UserName
	[string]$global:ipaddress = $(ipconfig | where {$_ -match 'IPv4.+\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' } | out-null; $Matches[1])
	$message = "Found username " + $global:UserName + " with ip address " + $global:ipaddress
	AddLog $message 2

	[string]$strXMLLine = "&amp;lt;uid-message&amp;gt;&amp;lt;version&amp;gt;1.0&amp;lt;/version&amp;gt;&amp;lt;type&amp;gt;update&amp;lt;/type&amp;gt;&amp;lt;payload&amp;gt;&amp;lt;login&amp;gt;&amp;lt;entry name=""" + $global:strDomain + "\" + $global:UserName + """ ip=""" + $global:ipaddress+ """ timeout=""" + $global:strTimeout + """/&amp;gt;&amp;lt;/login&amp;gt;&amp;lt;/payload&amp;gt;&amp;lt;/uid-message&amp;gt;"
	[string]$strEncodedXMLLine = [uri]::EscapeDataString($strXMLLine)

	$message = "Posting mapping to firewall " + $global:strFirewallIP + ": " + $strXMLLine
	AddLog $message 2
	[string]$url = "https://" + $global:strFirewallIP + "/api/?key=" + $global:strFirewallAPI + "&amp;amp;type=user-id&amp;amp;vsys=" + $global:strVsys + "&amp;amp;cmd=" + $strEncodedXMLLine
	[System.Net.HttpWebRequest]$request = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($url)
	$request.Method = "POST"
	$request.ContentType = "text/xml"
	[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
	$message = "Finished Post."
	AddLog $message 2
	try
	{
		[System.Net.HttpWebResponse]$response = [System.Net.HttpWebResponse]$request.GetResponse()     
		$sr = New-Object System.IO.StreamReader($response.GetResponseStream())       
		[Xml]$xmlResponse = $sr.ReadToEnd()
		if ([string]$xmlResponse.FirstChild.status -eq "error") 
		{
			$message = "Error mapping " + $global:ipaddress + " to user '" + $global:strDomain + "\" + $global:UserName + "' on firewall " + $global:strFirewallIP + ": " + $XmlDocument.FirstChild.result.msg
			AddLog $message
		}
		else
		{
			$message = "Sucessfully mapped " + $global:ipaddress + " to user '" + $global:strDomain + "\" + $global:UserName + "' on firewall " + $global:strFirewallIP
			AddLog $message 1
		}
	}			
	catch [Net.WebException] 
	{
		[System.Net.HttpWebResponse] $resp = [System.Net.HttpWebResponse] $_.Exception.Response  
		$message = "Error:" + [string]$resp
		AddLog $message
	}				
	
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
	[string]$message = $FailedItem + " failed with message " + $ErrorMessage
	AddLog $message
    Break
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Keep in mind! The script should run under the logged in users credentials (otherwise it would detect the computers login name or a scheduled login name).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Furthermore: The user should be able to access the script, which is a potential risk, cause this alse reveals the API key to the firewall.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway: this script should get you started.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can check or the XML request was successful in the log file and on the firewall (run “show user ip-user-mapping ip [ip]”)&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jan 2017 13:05:25 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/user-id-agnet-get-api-by-vbscript/m-p/135360#M1331</guid>
      <dc:creator>Sjoerd</dc:creator>
      <dc:date>2017-01-02T13:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: USER-ID agnet get API by VBscript</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/user-id-agnet-get-api-by-vbscript/m-p/143715#M1343</link>
      <description>&lt;P&gt;Thanks for the code, but as you know not all windows have the powershell&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Also I need to use API on userID windows agents not the firewall itself&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got vbscript that can get the info I want and save it in a test file, now I might use the powershell in the userid agent machine and get that text file from the users (or maybe I will let the script in the user machine send it to userid agent machine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it is very simple and I'm still modifying&amp;nbsp;it&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;strMsg = ""
strComputer = "."

Set objNetwork = CreateObject("Wscript.Network")
Set objWMIService = GetObject("winmgmts:" &amp;amp; "{impersonationLevel=impersonate}!\\" &amp;amp; strComputer &amp;amp; "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'")

For Each IPConfig in IPConfigSet
 If Not IsNull(IPConfig.IPAddress) Then
 For i = LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
  If Not Instr(IPConfig.IPAddress(i), ":") &amp;gt; 0 and ( Instr(IPConfig.IPAddress(i), "X.X") &amp;gt;0 or Instr(IPConfig.IPAddress(i), "X.Y") &amp;gt;0) Then
  strMsg = strMsg &amp;amp; IPConfig.IPAddress(i) &amp;amp; " " &amp;amp; objNetwork.UserName &amp;amp; vbcrlf
  End If
 Next
 End If
Next
WScript.Echo strMsg&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2017 22:18:36 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/user-id-agnet-get-api-by-vbscript/m-p/143715#M1343</guid>
      <dc:creator>mikealanni</dc:creator>
      <dc:date>2017-02-17T22:18:36Z</dc:date>
    </item>
  </channel>
</rss>

