Powershell:字符串变量不是有效的十六进制字符串错误

时间:2015-06-25 11:58:16

标签: powershell

我正在尝试创建一个powershell脚本,在Windows服务器上提取证书的指纹,以便我可以在变量中使用此指纹在另一个创建HTTPS侦听器的命令中使用它。

我已设法创建使用以下命令的第一步:

$thumbprint = (get-childitem -path cert:\localmachine\my | where-object {$_.subject -match $hostname+".xxx.com"}).thumbprint

所以现在我将指纹保存在一个变量中。

然后我尝试使用以下命令,如果我手动输入$ thumbprint值的内容,该命令有效:

winrm create winrm/config/listener?Address=*+Transport=HTTPS '@{Hostname="$hostname";CertificateThumbprint="$thumbprint"}'

运行此命令时出现以下错误:

Message = The WS-Management service cannot process the configuration request because the certificate thumbprint in the request is not a valid hex string: $thumbprint.

有谁知道如何解决这个问题?

这有效(手动输入相同的值):

winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname="xxx.xxx.xxx";CertificateThumbprint="AF1D0F82070C4E3692BBF43747BAE74DED74A40A"}'

$thumbprint变量的内容为AF1D0F82070C4E3692BBF43747BAE74DED74A40A

2 个答案:

答案 0 :(得分:4)

由于您使用的是PowerShell,因此可以使用WSMan PowerShell提供程序来配置WS-Management服务:

New-Item WSMan:\localhost\Listener -Address * -Transport HTTPS -HostName $hostname -CertificateThumbPrint $thumbprint

答案 1 :(得分:1)

由于@{}部分由单引号括起,因此PowerShell不会扩展$thumbprint之类的变量,因此您不必将指纹传递给winrm,而是将其转移到$thumbprint。重新传递字面值-f

您可以使用@{}运算符替换单引号字符串中的占位符。占位符编号并用大括号括起来,因此您还需要转义$WinRMConfig = '@{{Hostname="$hostname";CertificateThumbprint="{0}"}}' -f $thumbprint winrm create winrm/config/listener?Address=*+Transport=HTTPS $WinRMConfig 括号:

try {
            connection = connectionFactory.createConnection();
            connection.start();
            LOGGER.info(STARTED_CONNECTION_WITH_THE_DESTINATION + destinationName);
            session = createSession();
            destination = session.createQueue(destinationName);
            LOGGER.info(CREATED_QUEUE_IN_DESTINATION + destinationName);
            if (isImageProcAgent) {
                consumer = createConsumer();
                LOGGER.info(CONSUMER_HAS_BEEN_INITIALIZED);
            } else {
                producer = session.createProducer(destination);
                LOGGER.info(PRODUCER_HAS_BEEN_INITIALIZE);
            }
        } catch (MessagingException e) {
            LOGGER.error(e);
        } catch (JMSException e) {
            LOGGER.error(e);
        }