小编Fro*_* F.的帖子

任务计划 Powershell 脚本不显示消息框

我有一个 Powershell 脚本,计划在多个主机上运行过夜。它由任务计划程序中设置的 CMD 脚本(包装器)执行。

在某些情况下,我需要向用户显示一条消息,该消息将在早上到达,让他决定是继续执行脚本还是中止脚本。

我正在使用 system.windows.froms.MessageBox::Show("My Message","status","4") 来显示消息。

当我从 Windows PowerShell ISE 控制台执行 CMD 脚本时,它工作得很好,但是,如果我从任务计划程序执行它,它会跳过弹出消息。

这个问题有什么解决办法吗?

powershell messagebox taskscheduler

4
推荐指数
1
解决办法
3617
查看次数

屏幕和变量中的调用表达式输出

我试图将输出存储Invoke-expression到一个变量以及在屏幕上。我有PS日志记录,它会自动将所有内容记录为Write-Host一个文件。现在我正在使用Invoke-Expression它似乎在屏幕上打印输出或到一个变量,我都需要

我所尝试的是:

$var = "C:\ER\work\Canny.exe -Init ER\ER2 Get-ip"

$val = Invoke-Expression $var
Run Code Online (Sandbox Code Playgroud)

这不会在屏幕上打印任何内容,因此我无法确定运行时是否有任何问题。后来我做Write-Host$val这些记录,但它有时来不及知道发生了什么如果我使用:

Invoke-Expression $var
Run Code Online (Sandbox Code Playgroud)

没有任何记录(很明显),但是有控制台输出,如果我想在一段时间后查看日志,会发现发生了什么,我无法调查。我也尝试过:

Invoke-Expression $var -OutVariable out 
Run Code Online (Sandbox Code Playgroud)

要么

Invoke-Expression $var -OutVariable $out 
Run Code Online (Sandbox Code Playgroud)

这里没有用。我还创建了一个脚本块并尝试

Invoke-Command 
Run Code Online (Sandbox Code Playgroud)

再次没有用,我只需要它在屏幕上以及变量上打印输出。

powershell

4
推荐指数
1
解决办法
1万
查看次数

如何在BAThell中将分隔的字符串转换为嵌套的哈希表?

我正在努力寻找一种巧妙的方法将分隔的字符串转换为哈希表.例如给定字符串:

UK_Kent_Margate
Run Code Online (Sandbox Code Playgroud)

我想将其转换为PowerShell HashTable,如下所示:

$result = @{
    UK = @{
        Kent = @{
            Margate = @{}
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

因此,我可以使用"_"字符上的拆分轻松地将字符串分解为数组,但后来我正在努力(读卡住!)进行测试并在结果哈希中声明每个嵌套的哈希值.我想我需要一个没有问题的递归函数,但我无法理解如何在结果哈希中测试正确的级别.

由于我正在编写的应用程序可以有任意数量的'_'因此嵌套我需要想出一个光滑的方法来做这个,我想不出怎么做.

有没有人之前遇到过这样的事情并有任何建议?

powershell hashtable

3
推荐指数
1
解决办法
671
查看次数

管理员权限的通用测试

是否有单一的保证方法来测试当前用户是否具有管理员权限?我试过这个

$isAdmin = (new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole("Administrators")
Run Code Online (Sandbox Code Playgroud)

只要 Windows 最初是用英文安装的,它就可以工作。如果 Windows 以西班牙语安装,您必须测试 Administra d ors。还有一些其他语言的工作方式类似。我的第一个想法是测试所有可能的拼写,但如果有一些简单、优雅和万无一失的东西,那将是我的首选。

powershell

3
推荐指数
1
解决办法
1071
查看次数

如何验证xml的语法/格式是否正确(?)

假设我想在我的自定义工具箱模块中创建一个函数,该函数使用一些自定义xml文件来提供指令(使用$xmlpath参数传递).如何确保输入使用正确的xml类型/语法/格式(正确的元素,根节点等)?我读过有关模式(xsd)和命名空间(不明白这一点).

如果我使用模式文件:如何使用ex验证xml.Test-XML(PSCX cmdlet)?我是否在线存储xsd文件并在xml文档中指定它的路径?我将它存储在我的modules-folder中并将其硬编码到脚本中吗?如果是这样,我如何指定架构路径?Codesample:

#Stored in ..\Modules\MyModule\Process-Config.psm1)
function Process-Config
{
    param($xmlpath)
    #Test that file is xml
    try{ $xml = [xml](Get-Content $xmlpath) } catch {}

    #Test that xml-file is valid against schema in Module-folder (Modules\MyModule\xmlschema.xsd)
    #Using Test-XML PSCX cmdlet
    Test-XML -Path $xmlpath -SchemaPath #Schemapath#

}
Run Code Online (Sandbox Code Playgroud)

编辑:我发现$ PSScriptRoot提供了模块位置,所以我可以使用$ PSScriptRoot\Schemas\MySchema.xsd作为路径.我认为它只适用于脚本,但似乎功能也起作用.:-)仍然,我在xml中为这样的本地文件指定模式吗?如果是这样,我如何指定此本地路径,或者我是否应该在线发布xsd,以将其作为xml文件中的模式地址?

xml powershell powershell-3.0

2
推荐指数
1
解决办法
1万
查看次数

Windows PowerShell 2.0绝对是初学者 - 本书最后一个项目的错误

通过Jerry Lee Ford Jr.的Powershell 2.0 for Absolute Beginner一书.本书的代码中有很多印刷错误,我已经能够通过并纠正(很棒的练习),但有一个我根本无法弄清楚.从配套网站下载了他的股票代码,他的代码抛出完全相同的错误.代码如下:

# *************************************************************************
#
# Script Name: GameConsole.ps1 (The PowerShell Game Console)
# Version:     1.0
# Author:      Jerry Lee Ford, Jr.
# Date:        January 1, 2007
# 
# Description: This PowerShell script provides a listing of PowerShell
#              game scripts and allows the player to play any game by 
#              entering its menu number.
# 
# *************************************************************************


# Initialization Section

$menuList = @()  #Stores an array containing information about script games
$playAgain = …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0

1
推荐指数
1
解决办法
2697
查看次数

在powershell脚本中将文件从一个位置复制到另一个位置,另外还要检查某些值

所以我在某个位置有文件夹

C:\Users\ainfowara\Desktop\testfiles
Run Code Online (Sandbox Code Playgroud)

所以我想将这些文件移动到这个位置

C:\Users\ainfowara\Desktop\destinationTestfiles
Run Code Online (Sandbox Code Playgroud)

"testfiles"有这种格式的文件,txt.*.test.*所以基本上我想在我移动文件之前检查它们在第三部分中有那两个主要的东西(txt)和(test).

有人可以帮助我,如何在powershell脚本中执行此操作

我知道我可以这样做,设置文件夹路径

path_src= C:\Users\ainfowara\Desktop\testfiles
path_dst= C:\Users\ainfowara\Desktop\destinationTestfiles
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助

directory powershell file

1
推荐指数
1
解决办法
3万
查看次数

如何从powershell中的命令行运行"hello world"脚本?

我无法传递此错误消息.

谢谢

The term 'power-date.ps1' is not recognized as the name of a cmdlet, function, script file
, or operable program. Check the spelling of the name, or if a path was included, verify t
hat the path is correct and try again.
At line:1 char:15
+ power-date.ps1 <<<< 
    + CategoryInfo          : ObjectNotFound: (power-date.ps1:String) [], CommandNotFound 
   Exception
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

powershell

0
推荐指数
1
解决办法
417
查看次数

Powershell:将日期更改为int吗?

我有一个cmdlet,它期望int32日期而不是标准化输入。

Set-CTXGroupPolicyConfigurationRebootScheduleStartDate希望int32-input。例如,如果我手动将其输入到策略中,则明天1/18/2015132055314。将数字甚至表示空白。

powershell

0
推荐指数
1
解决办法
3106
查看次数