无法使用 PowerShell 从 windows 服务器 2019 连接到服务总线
Unable to connect to service-bus from windows server 2019 using PowerShell
我在 PowerShell 中有下面提到的代码。此代码在其中一台服务器中不起作用,因为流量未通过代理并被防火墙阻止。相同的代码在另一台服务器上运行良好,唯一的区别是流量会自动路由到代理服务器。
我比较了 "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
在工作和不工作的服务器中具有相同的值不知道缺少什么为什么糟糕的服务器流量直接路由到防火墙而不是通过代理
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
Import-Module "C:\Program Files\Microsoft Integration Runtime\5.0\Gateway\Microsoft.ServiceBus.dll"
$ConnectionString = "ConnectionString"
$blobPath = "blobPath"
$parentPath = "2047895"
$QueueClient = [Microsoft.ServiceBus.Messaging.TopicClient]::CreateFromConnectionString($ConnectionString)
$String = '{
"EventId": "11111",
"EventType": "SampleReadyForSync",
"EventDescription": "Samples staged sucessfully",
"lob": "lob",
"inputType":"folder",
"source": "'+ $blobPath + "" + $parentPath + '"
}'
$String = $String.Substring(0, [System.Math]::Min(1000, $String.Length))
$Encoding = [system.Text.Encoding]::UTF8
$UTF8String = $Encoding.GetBytes($String)
$MemoryStream = New-Object IO.MemoryStream -ArgumentList $UTF8String, $true
$Message = New-Object Microsoft.ServiceBus.Messaging.BrokeredMessage -ArgumentList $MemoryStream
$Message.MessageId = [Guid]::NewGuid()
$Message.Label = "AC"
$QueueClient.Send($Message)
我已经试过像下面那样强制代理,但仍然没有成功,流量仍然直接进入互联网并被防火墙阻止。
$proxyString = "proxyURL"
$proxyUri = new-object System.Uri($proxyString)
[System.Net.WebRequest]::DefaultWebProxy = new-object System.Net.WebProxy ($proxyUri, $true)
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
尝试了如下的另一个选项
$null = [Reflection.Assembly]::LoadWithPartialName("System.Web")
$ConnectionString = ''
$TokenValidFor = 3600
# This part may need editing, EntityPath is specific to connection strings from policies on a queue level
$Pattern = 'Endpoint=(.+);SharedAccessKeyName=(.+);SharedAccessKey=(.+);EntityPath=(.+)'
([uri]$Endpoint), $PolicyName, $Key, $Queue = ($ConnectionString -replace $Pattern, ';;;') -split ';'
$UrlEncodedEndpoint = [System.Web.HttpUtility]::UrlEncode($Endpoint)
$Expiry = [DateTimeOffset]::Now.ToUnixTimeSeconds() + $TokenValidFor
$RawSignatureString = "$UrlEncodedEndpoint`n$Expiry"
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
$HMAC.Key = [Text.Encoding]::ASCII.GetBytes($Key)
$HashBytes = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($RawSignatureString))
$SignatureString = [Convert]::ToBase64String($HashBytes)
$UrlEncodedSignatureString = [System.Web.HttpUtility]::UrlEncode($SignatureString)
$SASToken = "SharedAccessSignature sig=$UrlEncodedSignatureString&se=$Expiry&skn=$PolicyName&sr=$UrlEncodedEndpoint"
$Params = @{
Uri = "https://$($Endpoint.Host)/$Queue/messages"
ContentType = 'text/plain;charset=utf-8'
Method = 'Post'
Body = 'Hello, World!'
Headers = @{
'Authorization' = $SASToken
}
}
Invoke-RestMethod @Params
在本地工作正常,但从本地服务器收到如下错误
Invoke-RestMethod : The remote server returned an error: (500) Internal Server
我在一些博客中读到的另一个问题是我们必须像下面这样定义连接模式
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http
但我不确定如何在 PowerShell 中执行此操作
问题已通过将连接模式添加为 https 来解决。在我指定连接模式并开始数据摄取后,流量通过代理开始。
Import-Module "C:\Program Files\Microsoft Integration Runtime\5.0\Gateway\Microsoft.ServiceBus.dll"
$ConnectionString = "ConnectionString"
$blobPath = "blobPath"
$parentPath = "2047895"
**[Microsoft.ServiceBus.ServiceBusEnvironment]::SystemConnectivity.Mode = [Microsoft.ServiceBus.ConnectivityMode]::Https**
$QueueClient = [Microsoft.ServiceBus.Messaging.TopicClient]::CreateFromConnectionString($ConnectionString)
$String = '{
"EventId": "11111",
"EventType": "SampleReadyForSync",
"EventDescription": "Samples staged sucessfully",
"lob": "lob",
"inputType":"folder",
"source": "'+ $blobPath + "" + $parentPath + '"
}'
$String = $String.Substring(0, [System.Math]::Min(1000, $String.Length))
$Encoding = [system.Text.Encoding]::UTF8
$UTF8String = $Encoding.GetBytes($String)
$MemoryStream = New-Object IO.MemoryStream -ArgumentList $UTF8String, $true
$Message = New-Object Microsoft.ServiceBus.Messaging.BrokeredMessage -ArgumentList $MemoryStream
$Message.MessageId = [Guid]::NewGuid()
$Message.Label = "AC"
$QueueClient.Send($Message)
我在 PowerShell 中有下面提到的代码。此代码在其中一台服务器中不起作用,因为流量未通过代理并被防火墙阻止。相同的代码在另一台服务器上运行良好,唯一的区别是流量会自动路由到代理服务器。
我比较了 "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
在工作和不工作的服务器中具有相同的值不知道缺少什么为什么糟糕的服务器流量直接路由到防火墙而不是通过代理
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
Import-Module "C:\Program Files\Microsoft Integration Runtime\5.0\Gateway\Microsoft.ServiceBus.dll"
$ConnectionString = "ConnectionString"
$blobPath = "blobPath"
$parentPath = "2047895"
$QueueClient = [Microsoft.ServiceBus.Messaging.TopicClient]::CreateFromConnectionString($ConnectionString)
$String = '{
"EventId": "11111",
"EventType": "SampleReadyForSync",
"EventDescription": "Samples staged sucessfully",
"lob": "lob",
"inputType":"folder",
"source": "'+ $blobPath + "" + $parentPath + '"
}'
$String = $String.Substring(0, [System.Math]::Min(1000, $String.Length))
$Encoding = [system.Text.Encoding]::UTF8
$UTF8String = $Encoding.GetBytes($String)
$MemoryStream = New-Object IO.MemoryStream -ArgumentList $UTF8String, $true
$Message = New-Object Microsoft.ServiceBus.Messaging.BrokeredMessage -ArgumentList $MemoryStream
$Message.MessageId = [Guid]::NewGuid()
$Message.Label = "AC"
$QueueClient.Send($Message)
我已经试过像下面那样强制代理,但仍然没有成功,流量仍然直接进入互联网并被防火墙阻止。
$proxyString = "proxyURL"
$proxyUri = new-object System.Uri($proxyString)
[System.Net.WebRequest]::DefaultWebProxy = new-object System.Net.WebProxy ($proxyUri, $true)
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
尝试了如下的另一个选项
$null = [Reflection.Assembly]::LoadWithPartialName("System.Web")
$ConnectionString = ''
$TokenValidFor = 3600
# This part may need editing, EntityPath is specific to connection strings from policies on a queue level
$Pattern = 'Endpoint=(.+);SharedAccessKeyName=(.+);SharedAccessKey=(.+);EntityPath=(.+)'
([uri]$Endpoint), $PolicyName, $Key, $Queue = ($ConnectionString -replace $Pattern, ';;;') -split ';'
$UrlEncodedEndpoint = [System.Web.HttpUtility]::UrlEncode($Endpoint)
$Expiry = [DateTimeOffset]::Now.ToUnixTimeSeconds() + $TokenValidFor
$RawSignatureString = "$UrlEncodedEndpoint`n$Expiry"
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
$HMAC.Key = [Text.Encoding]::ASCII.GetBytes($Key)
$HashBytes = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($RawSignatureString))
$SignatureString = [Convert]::ToBase64String($HashBytes)
$UrlEncodedSignatureString = [System.Web.HttpUtility]::UrlEncode($SignatureString)
$SASToken = "SharedAccessSignature sig=$UrlEncodedSignatureString&se=$Expiry&skn=$PolicyName&sr=$UrlEncodedEndpoint"
$Params = @{
Uri = "https://$($Endpoint.Host)/$Queue/messages"
ContentType = 'text/plain;charset=utf-8'
Method = 'Post'
Body = 'Hello, World!'
Headers = @{
'Authorization' = $SASToken
}
}
Invoke-RestMethod @Params
在本地工作正常,但从本地服务器收到如下错误
Invoke-RestMethod : The remote server returned an error: (500) Internal Server
我在一些博客中读到的另一个问题是我们必须像下面这样定义连接模式
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http
但我不确定如何在 PowerShell 中执行此操作
问题已通过将连接模式添加为 https 来解决。在我指定连接模式并开始数据摄取后,流量通过代理开始。
Import-Module "C:\Program Files\Microsoft Integration Runtime\5.0\Gateway\Microsoft.ServiceBus.dll"
$ConnectionString = "ConnectionString"
$blobPath = "blobPath"
$parentPath = "2047895"
**[Microsoft.ServiceBus.ServiceBusEnvironment]::SystemConnectivity.Mode = [Microsoft.ServiceBus.ConnectivityMode]::Https**
$QueueClient = [Microsoft.ServiceBus.Messaging.TopicClient]::CreateFromConnectionString($ConnectionString)
$String = '{
"EventId": "11111",
"EventType": "SampleReadyForSync",
"EventDescription": "Samples staged sucessfully",
"lob": "lob",
"inputType":"folder",
"source": "'+ $blobPath + "" + $parentPath + '"
}'
$String = $String.Substring(0, [System.Math]::Min(1000, $String.Length))
$Encoding = [system.Text.Encoding]::UTF8
$UTF8String = $Encoding.GetBytes($String)
$MemoryStream = New-Object IO.MemoryStream -ArgumentList $UTF8String, $true
$Message = New-Object Microsoft.ServiceBus.Messaging.BrokeredMessage -ArgumentList $MemoryStream
$Message.MessageId = [Guid]::NewGuid()
$Message.Label = "AC"
$QueueClient.Send($Message)