我有一个脚本,我可以通过Invoke-Command远程运行
Invoke-Command -ComputerName (Get-Content C:\Scripts\Servers.txt) `
-FilePath C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1
Run Code Online (Sandbox Code Playgroud)
只要我使用默认参数,它就可以正常工作.但是,该脚本有2个名为[switch]的参数(-Debug和-Clear)
如何通过Invoke-Command传递切换参数?我已经尝试了-ArgumentList但是我遇到了错误所以我必须要有语法错误或者其他什么.任何帮助是极大的赞赏.
我对PowerShell比较陌生,并且有一个脚本可以读取一个配置文件,该文件会产生一组名称值对,我希望将它们作为参数传递给第二个PowerShell脚本中的函数.
我不知道在设计时将在此配置文件中放置哪些参数,所以就在我需要调用第二个PowerShell脚本时,我基本上只有一个变量具有第二个脚本的路径,第二个脚本变量,它是要传递给路径变量中标识的脚本的参数数组.
因此,包含第二个脚本($ scriptPath)路径的变量可能具有如下值:
"c:\the\path\to\the\second\script.ps1"
Run Code Online (Sandbox Code Playgroud)
包含参数($ argumentList)的变量可能类似于:
-ConfigFilename "doohickey.txt" -RootDirectory "c:\some\kind\of\path" -Max 11
Run Code Online (Sandbox Code Playgroud)
如何从这种状态到使用$ argumentList中的所有参数执行script.ps1?
我希望来自第二个脚本的任何write-host命令对于调用此第一个脚本的控制台是可见的.
我尝试过dot-sourcing,Invoke-Command,Invoke-Expression和Start-Job,但我还没有找到一种不会产生错误的方法.
例如,我认为最简单的第一条路线是尝试按如下方式调用Start-Job:
Start-Job -FilePath $scriptPath -ArgumentList $argumentList
Run Code Online (Sandbox Code Playgroud)
...但是由于此错误而失败:
System.Management.Automation.ValidationMetadataException:
Attribute cannot be added because it would cause the variable
ConfigFilename with value -ConfigFilename to become invalid.
Run Code Online (Sandbox Code Playgroud)
...在这种情况下,"ConfigFilename"是第二个脚本定义的参数列表中的第一个参数,我的调用显然试图将其值设置为"-ConfigFilename",这显然是为了按名称标识参数,没有设定它的价值.
我错过了什么?
编辑:
好的,这是一个名为invokee.ps1的文件中的待调用脚本的模型
Param(
[parameter(Mandatory=$true)]
[alias("rc")]
[string]
[ValidateScript( {Test-Path $_ -PathType Leaf} )]
$ConfigurationFilename,
[alias("e")]
[switch]
$Evaluate,
[array]
[Parameter(ValueFromRemainingArguments=$true)]
$remaining)
function sayHelloWorld()
{
Write-Host "Hello, everybody, the config file is <$ConfigurationFilename>."
if ($ExitOnErrors)
{
Write-Host "I should …Run Code Online (Sandbox Code Playgroud) powershell parameter-passing command-line-arguments invoke-command start-job
我的问题与此问题非常相似,除了我试图使用Invoke-Command捕获ScriptBlock的返回代码(因此我不能使用-FilePath选项).这是我的代码:
Invoke-Command -computername $server {\\fileserver\script.cmd $args} -ArgumentList $args
exit $LASTEXITCODE
Run Code Online (Sandbox Code Playgroud)
问题是Invoke-Command没有捕获script.cmd的返回码,所以我无法知道它是否失败.我需要知道script.cmd是否失败.
我也尝试使用New-PSSession(这让我看到了远程服务器上的script.cmd的返回代码)但是我找不到任何方法将它传递回我的调用Powershell脚本来实际处理有关失败的任何事情.
powershell return-value exit-code windows-scripting invoke-command
我想在a中按下ENTER时调用一个命令TextBox.考虑以下XAML:
<UserControl
...
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
...>
...
<TextBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction Command="{Binding MyCommand}"
CommandParameter="{Binding Text}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
...
</UserControl>
Run Code Online (Sandbox Code Playgroud)
并且MyCommand如下:
public ICommand MyCommand {
get { return new DelegateCommand<string>(MyCommandExecute); }
}
private void MyCommandExecute(string s) { ... }
Run Code Online (Sandbox Code Playgroud)
使用上面的命令,我会在每次按键时调用我的命令.如何将命令限制为仅在按下ENTER键时调用?
我理解使用Expression Blend我可以使用条件,但这些似乎仅限于元素,不能考虑事件参数.
我也遇到过SLEX,它提供了自己的InvokeCommandAction实现,它构建在Systems.Windows.Interactivity实现之上,可以满足我的需求.另一个考虑是编写我自己的触发器,但我希望有一种方法可以在不使用外部工具包的情况下完成.
我正试图通过powershell与服务进行通信,但我失败了.我怀疑这是证书,我搜索了答案,找到了两个选项,其中没有一个对我有用.我也尝试将两者合并失败.
选项1:
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;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$urlJSON = "https://internal.ad.local/path/api_jsonrpc.php"
#Create authentication JSON object using ConvertTo-JSON
$objAuth = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'user.authenticate' |
Add-Member -PassThru NoteProperty params @{user="user";password="password"} |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
Invoke-RestMethod -Uri $urlJSON -body $objAuth -method …Run Code Online (Sandbox Code Playgroud) 我意识到当使用与EventTrigger关联的InvokeCommandAcction时,原始事件仍然会路由到父元素,直到它被处理.好吧,我想这是一种预期的行为.但我的问题是如何将事件标记为Handled,以便它不会在整个UI树中传播?
实际上,当您在命令中处理此事件时,所有内容都将在此命令中处理,因此不需要传播.在我发现的一个案例中,它会导致一些不必要的行为.例如,当用户双击元素(MouseDoubleClick事件)时,我打开一个新窗口.问题是新窗口打开然后主窗口返回到新窗口的前面,因为MouseDoubleClick事件刚刚到达UI树中的顶部元素.想要的行为是保持新窗口在前面,但是当InvokeCommandAction让事件向上传播时,主窗口会收回焦点......
我可以做的是使用CallMethodAction资产,但因为我在MVVM场景中,我不想在我的代码中使用UI事件参数.即使这会让我隐含地将事件标记为已处理并解决问题.
<UserControl x:Class="..."
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding Path=DisplayReportCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
</UserControl>
Run Code Online (Sandbox Code Playgroud) 我已经尝试了很多不同的方法来做到这一点,它们都会导致同样的错误.这是一组命令:
$s = New-PSsession -ComputerName ServerA
$job = Invoke-Command -Session $s -Scriptblock { Start-Process -FilePath
"C:\Scripts\ArchiveEventLogs\ver4.5\7za.exe" -ArgumentList "a",
"C:\Scripts\Eventlogs.bak\ServerA-20101111.7z", "C:\Scripts\Eventlogs.bak\*.evt*",
"-mx7", "-oC:\Scripts\Eventlogs.bak", "-wC:\Scripts\Eventlogs.bak", "-t7z" -Wait }
-AsJob
Get-Job | Wait-Job
Receive-Job Job$
Run Code Online (Sandbox Code Playgroud)
我得到的输出是这样的:
7-Zip (A) 9.17 beta Copyright (c) 1999-2010 Igor Pavlov 2010-10-04
Scanning
Creating archive C:\Scripts\Eventlogs.bak\ServerA-20101111.7z
ERROR: Can't allocate required memory!
Run Code Online (Sandbox Code Playgroud)
我怎么能通过那个错误?
我应该指出,如果我直接在没有远程处理的ServerA上运行Scriptblock,它就可以工作.谢谢你的帮助!
设置Mac OSX 10.6.8,Apache Tomcat 6.0.16,Java1.6.0_29,Eclipse IDE Indigo.
我之前在如何执行和包含来自Web App项目的Java API中向我提出了类似的问题但是设置已经改变了,因为我现在在WebAp中有Java代码
我试图从JSP页面调用Java方法并返回结果.我看了很多帖子,但我担心我用这两种语言缺乏经验是主要问题.
我有一个JSP WebAp,它搜索XML数据库并将内容返回给用户.我被要求集成其他搜索预定义网站并返回内容的Java代码.
我玩过类似的代码到下面,但我想我正在寻找别的东西
<%@ include file="/Applications/Tomcat/apache-tomcat-6.0.16/webapps/myWebApp/program.java" %>
Run Code Online (Sandbox Code Playgroud)
谁能让我更好地了解我在寻找什么?
另外你知道我是否必须在WebAp中编辑其他任何东西来连接这两个文件?WebINF文件夹中的类文件?
很感谢任何形式的帮助
困境
package sliceClient;
import java.util.List;
public class Run {
@Inject
private SliceSearcher sliceSearcher;
@Inject
private SliceCreator sliceCreator;
/**
* @param args
*/
public static void main(String[] args) {
Injector injector = Guice.createInjector(new GuiceInjector());
Run r = injector.getInstance(Run.class);
r.runSliceConsumer();
}
private void setSlicepediaProductionMode(boolean productionMode){
sliceSearcher.setProductionMode(productionMode);
sliceCreator.setProductionMode(productionMode);
}
public void runSliceConsumer() {
System.out.println("Starting Slice Consumer");
//Remove this line if …Run Code Online (Sandbox Code Playgroud) 好的,这是我的问题:
我试图在服务器上远程运行脚本.
我是两个盒子的管理员,防火墙例外已经到位,启用了远程管理,其他一切看起来都很好,我可以看到.
invoke-command -ComputerName $ComputerName -ScriptBlock `
{
cd C:\Windows\System32\inetsrv\;
./appcmd.exe ADD vdir /app.name:<SiteName>/ /path:/<VDir Name> /physicalPath:<Path to files>
}
Run Code Online (Sandbox Code Playgroud)
我一直收到以下错误
ERROR ( hresult:80070005, message:Failed to commit configuration changes. Access is denied.
Run Code Online (Sandbox Code Playgroud)
它试图运行的服务器是服务器2k8 R2盒子,我认为这个问题是UAC问题.无论如何要让它以管理员身份运行而不必在UAC盒子上单击是吗?
这段代码最终将成为一个必须完全自动化的脚本.
任何帮助将不胜感激.
我试图使用invoke-command在远程计算机上执行exe.使用远程桌面登录计算机后,在远程计算机上执行exe需要1GB内存,并在一分钟后执行完成.而当我在同一台机器上使用Invoke-Command执行相同的exe时,进程返回OutOfMemoryException并突然结束.我的invoke命令很简单Invoke-Command -Session $someSessionVariable -ScriptBlock {Invoke-Expression "abc.exe --arg arg"} -AsJob.
我是否遗漏了有关远程调用限制的内容?
提前致谢.
invoke-command ×10
powershell ×7
7zip ×1
appcmd ×1
command ×1
eventtrigger ×1
exit-code ×1
https ×1
java ×1
jsp ×1
mvvm ×1
return-value ×1
self-signed ×1
silverlight ×1
ssl ×1
start-job ×1
tomcat ×1
uac ×1
wpf ×1
xaml ×1