Powershell appcmd.exe尝试执行

时间:2019-01-17 15:45:22

标签: powershell appcmd

我要为appPools添加一些环境吗?我尝试了这段代码:

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace BarcodeReceivingApp
{
    public partial class BarcodeReceivingForm : Form
    {
        public BarcodeReceivingForm()
        {
            InitializeComponent();

        }

        private Thread _readWriteThread;
        private TcpClient _client;
        private NetworkStream _networkStream;
        private void btn_ConnectToTelnetPort_Click(object sender, EventArgs e)
        {
            // connection tcp/ip
            const string hostname = "myipaddress";
            const int port = 23;
            ServerSocket(hostname, port);
            //Connect();

        }


        public void ServerSocket(string ip, int port)
        {
            try
            {
                _client = new TcpClient(ip, port);
                lbl_ConnectionMessage.Text = @"Connected to server.";
            }
            catch (SocketException)
            {
                MessageBox.Show(@"Failed to connect to server");
                return;
            }

            //Assign networkstream
            _networkStream = _client.GetStream();

            //start socket read/write thread
            _readWriteThread = new Thread(ReadWrite);
            _readWriteThread.Start();
        }

        private void ReadWrite()
        {
            var received = "";

            //Read first thing givent o us
            received = Read();
            //Console.WriteLine(received);
            txt_BarcodeDisplay.Text = received + Environment.NewLine;

            //txt_BarcodeDisplay.Text = recieved.ToString();


            //Set up connection loop
            while (true)
            {
                var command = btn_StopConnection.Text;
                if (command == "STOP1")
                    break;

                //write(command);
                received = Read();
                //Console.WriteLine(received);
                txt_BarcodeDisplay.Text += received + Environment.NewLine;
            }
            // possible method to end the port connection


        }

        public string Read()
        {
            byte[] data = new byte[1024];
            var received = "";

            var size = _networkStream.Read(data, 0, data.Length);
            received = Encoding.ASCII.GetString(data, 0, size);

            return received;
        }

        private void btn_StopConnection_Click(object sender, EventArgs e)
        {
            _networkStream.Close();
            _client.Close();
        }
    }
}

但是第二行中的$ Task不起作用,如何将变量传递到此字符串?我也尝试了%Task%

1 个答案:

答案 0 :(得分:0)

.eProto_Pool$Task的属性。如果要在字符串中取消引用(即检索对象的单个属性),则需要使用$()(PowerShell中的子表达式运算符)包装字符串。

例如,我将创建一个具有两个属性的名为$MyString的新哈希表。

$MyString = @{Name = "Stephen";Value="CoolDude"}

>$MyString

Name                           Value
----                           -----
Value                          CoolDude
Name                           Stephen

看看如果我尝试使用常规字符串扩展在字符串中引用它会发生什么。这基本上就是您在上面的示例中所做的。看看它如何无法按预期工作?

write-host " The user $MyString.Name is a $MyString.Value"
 The user System.Collections.Hashtable.Name is a System.Collections.Hashtable.Value

使用子表达式运算符保存日期的时间。

write-host " The user $($MyString.Name) is a $($MyString.Value)"
 The user Stephen is a CoolDude

如有疑问,请对其进行子表达。

乍一看

我认为可能是百分号%引起了您的悲伤。这是PowerShell中ForEach-Object命令的简写。尝试以下方法:

Invoke-expression "$appcmd --% set config -section:system.applicationHost/applicationPools /+`"`"[name='$($Task.eProto_Pool)'].environmentVariables.[name='PRODUCT_NAME',value='eProto']`"`" /commit:apphost`""

这应该根据需要转义字符串,并像eProto_Pool的{​​{1}}属性那样传递参数。