如何创建一个消息框作为powershell.exe的参数?

时间:2017-11-14 07:35:17

标签: powershell

如何为powershell.exe提供参数以生成消息框?这里的关键短语是powershell.exe的参数,不是来自.ps1脚本,也不是来自Powershell提示本身。我目前有这个,但它产生了错误:

def Ghr(time, Rn):
    """Soil Heat flux calculator"""

    #sunrise -sunset hours from 1st of the month (sr,ss) to end of the month
    #Jan-1st SR 8:17, Jan-31st SR 07:47
    #Jan-1st SS 16:03, Jan-31st SS 16:52  

    daylight = {'Jan':('08:00', '16:25'),
            'Feb':('07:20', '17:20'),
            'Mar':('06:45', '18:40'),
            'Apr':('06:05', '20:05'),
            'May':('05:10', '20:55'),
            'Jun':('04:50', '21:25'),
            'Jul':('05:10', '21:15'),
            'Aug':('05:50', '20:30'),
            'Sep':('06:45', '19:25'),
            'Oct':('07:00', '17:30'),
            'Nov':('07:25', '16:15'),
            'Dec':('08:05', '16:00')}

    #this maps the month of 'time' as to the dictionary daylight
    #then unzips the tuple to daystart and dayend lists which 
    #are then converted to pandas.Series objects for use in boolean mask
    daystart, dayend = zip(*time.dt.strftime('%b').map(daylight))

    #dt.strftime extracts the 'Mon' ('%b') from the DateTime object and
    #maps it to the dictionary and saves the morning and dusk hours respectively

    #conversion to pandas series
    daystart=pd.Series(pd.to_datetime(daystart).time)
    dayend=pd.Series(pd.to_datetime(dayend).time)
    t=pd.Series(time.dt.time)
    #the above converts the strings to datetime objects and saves it
    #to a pandas Series, t is just a to_Series conversion


    light_mask = t.between(daystart, dayend)
    #this mask will be used to designate which values get multiplied by
    #either .1 or .5


    #True values get multiplied by .1 (The inversion is because 
    #pd.Series.where() only changes the values which are False)
    Ghr = Rn.where(np.invert(light_mask), Rn*.1)

    #False values multiplied by *.5
    Ghr = Rn.where(light_mask, Ghr*.5)

    return Ghr

我也试过没有powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"); [System.Windows.Forms.MessageBox]::Show("Test!!!")" -Command,有或没有双引号。

创建错误:

Invoke-Expression

1 个答案:

答案 0 :(得分:1)

这是报价问题。在参数及其内容中使用相同的双引号"会弄乱内容。作为解决方法,在Powershell命令中使用单引号,并在整个-Command参数周围使用双引号。像这样,

powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Test!!!')"

话虽如此,Add-Type -AssemblyName是IMAO加载程序集的更漂亮的方式。像这样,

powershell.exe -Command "add-type -assemblyname System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('Test!!!')"