我经常使用cmdlbinding我的函数或脚本,但总是发现这些东西很深奥,也许你们中的一些人可以分享他们的灯光.
根据get-help about_Functions_CmdletBindingAttribute
CmdletBinding属性是函数的一个属性,使它们像编译的cmdlet一样运行
但我们可以在我们的脚本之上使用它,在这种情况下的功能是什么?ps引擎为其所有输入调用的内部隐式"main"函数?
关于语法现在:
[CmdletBinding(ConfirmImpact=<String>,
DefaultParameterSetName=<String>,
HelpURI=<URI>,
SupportsPaging=<Boolean>,
SupportsShouldProcess=<Boolean>,
PositionalBinding=<Boolean>)]
Run Code Online (Sandbox Code Playgroud)
我们在做什么 ?实例化cmdlbinding对象并为其构造函数提供参数列表?这个语法可以在param()中找到,例如param()这个语法是否有特定的名称,可以在其他地方找到吗?
最后,作为简单的powershellers,我们能够通过设置属性来模仿这个功能并修改脚本的行为吗?
我正在编写一个执行Powershell脚本的批处理文件,该脚本在某一时刻将具有UNC路径的项作为属性循环并Get-ChildItem在这些路径上使用.在最小版本中,这是我的脚本中发生的事情:
Master.bat
powershell -ExecutionPolicy ByPass -File "Slave.ps1"
Run Code Online (Sandbox Code Playgroud)
Slave.ps1
$foo = @{Name = "Foo"}
$foo.Path = "\\remote-server\foothing"
$bar = @{Name = "Bar"}
$bar.Path = "\\remote-server\barthing"
@( $foo, $bar ) | ForEach-Object {
$item = Get-ChildItem $_.Path
# Do things with item
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我运行Master.bat时,它会因为Get-ChildItem错误而失败
get-childitem : Cannot find path '\\remote-server\foothing' because it does not exist.
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用Powershell直接运行Slave.ps1文件,它似乎完全正常.为什么只有在运行Master.bat文件时才会发生这种情况?
我尝试过的事情
FileSystem::提供商之前添加UNC路径http://powershell.org/wp/2014/02/20/powershell-gotcha-unc-paths-and-providers/-literalPath参数而不是plain -path参数Get-ChildItemGet-ChildItem \\remote-server\foothing在PowerShell中运行并成功验证与远程服务器的连接我有一个工作脚本,允许我解锁用户帐户(通过将lockouttime AD属性设置为0)这样的事情:
$entry["lockouttime"][0]=0;
$mod=ldap_mod_replace($ds,$dn,$entry)
Run Code Online (Sandbox Code Playgroud)
现在我想做相反的事情:锁定帐户.我已经读过lockouttime是一个系统属性,而active目录不允许我们将其值设置为0.
所以我试图用用户帐户和密码错误绑定到服务器,但这似乎不起作用.
for($i=0;$i<10;$i++){
ldap_bind($ds,$dn, "theWrongPasswd");
}
Run Code Online (Sandbox Code Playgroud)
运行此将显示此错误
Warning: ldap_bind(): Unable to bind to server: Invalid credentials
Run Code Online (Sandbox Code Playgroud)
但该帐户仍然可以解锁.
你知道我怎么能这样做吗?提前致谢.
我正在尝试制作一个vb6 prog来等待创建一个pdf文件.现在我只是暂停3秒:
startTime = Time
endTime = TimeValue(startTime) + TimeValue(TimeSerial(0,0,3))
While endTime > Time
Wend
If FSO.FileExists(sPdfFileName) Then
OkCreatedPDF = True
Else
OkCreatedPDF = False
End If
Run Code Online (Sandbox Code Playgroud)
但有时候pdf创作花费的时间超过3秒.所以我想等待创建文件,但是超时(10秒).我不想延长等待时间,因为这将会运行一千次.
我想知道如何在我的所有同事之间分享并保持最新的个人资料($ profile.AllUsersAllHosts).因为我是团队中唯一一个制作POSH脚本的人,所以我想创建一种框架,将其分发给团队成员.
有没有办法从网址加载配置文件?或者我应该使用本地profil映射网络驱动器,然后加载.ps1文件?其他想法?
谢谢
我正在编写一个解析来自pop3邮箱的电子邮件的应用程序.我已经提取了消息的附加文件,现在我想转换消息文本中的链接.
这意味着我有这个:src="cid:image001.png@01CC9ED6.84327130"
我想要的东西:src="http://xxx/image001.png"
你能帮助我吗?
preg_replace('/cid:/', 'http://xxx')现在如何删除'@'后的序列?
谢谢
我有这个功能可以在我的活动目录中进行身份验证,它的工作正常,除了一些包含特殊字符的密码,例如::
Xefeéà&"+
总是返回“无效的凭证”
我在互联网上看过几篇文章,但似乎都没有正确地避开字符。
我正在使用PHP版本5.3.8-ZS(Zend服务器)
这是我的类构造函数中的设置:
ldap_set_option($this->_link, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($this->_link, LDAP_OPT_REFERRALS, 0);
Run Code Online (Sandbox Code Playgroud)
和登录功能:
public function login($userlogin,$userpassword){
$return=array();
$userlogin.="@".$this->_config["domaine"];
$ret=@ldap_bind($this->_link , $userlogin ,utf8_decode($userpassword));
if(!$ret){
$return[]=array("status"=>"error","message"=>"Erreur d'authentification ! <br/> Veuillez vérifier votre nom et mot de passe SVP","ldap_error"=>ldap_error($this->_link));
}
if($ret)
{
$return[]=array("status"=>"success","message"=>"Authentification réussie");
}
return json_encode($return);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
我正在尝试运行批处理脚本来获取基本的计算机信息,如 CPU、RAM 和活动网卡。这是我的代码
@Echo OFF
set newline=^& echo.
echo Manufacturer Information> test1.txt
systeminfo|findstr /c:"Host Name" /c:"OS Name" /c:"System Model:" /c:"System Type:" /c:"Total Physical Memory:" >>test1.txt
echo CPU Information:>> test1.txt
wmic cpu get Name /Format:list >> test1.txt
echo %newline%Process Information:>> test1.txt
wmic computersystem get NumberofProcessors /Format:list >> test1.txt
echo %newline%NIC Information:>> test1.txt
wmic nicconfig where "IPEnabled=TRUE" get ipaddress, macaddress,defaultipgateway /format:list >>test1.txt
Run Code Online (Sandbox Code Playgroud)
输出外观:
制造商信息 主机名:DK-IT 操作系统名称:
Microsoft Windows 8.1 单语言系统型号:
Inspiron 7537 系统类型:基于 x64 的 PC 总物理内存:6,043 MB CPU 信息:Run Code Online (Sandbox Code Playgroud)N a …
有没有人知道为什么Remove-Item在Delete工作时会失败?
在下面的脚本中,我得到了一个我想删除的文件列表.
使用Remove-Item我收到以下错误消息:
VERBOSE:在目标"\\ UncPath\Folder\test.rtf"上执行"删除文件"操作.删除项目:无法删除项目\\ UncPath\Folder\test.rtf:拒绝访问该路径.
但是使用Delete是在我们说话时删除这些文件.
脚本
$files = gci \\UncPath\Folder| ?{ $_.LastWriteTime -le (Get-Date).addDays(-28) }
# This doesn't work
$files | Remove-Item -force -verbose
# But this does
$files | % { $_.Delete() }
Run Code Online (Sandbox Code Playgroud) 我需要一个更好的方法来做这个想法吗?
$strOutput = "800x600, 32 bits @ 60 Hz."
# Initial split
$aSplitString = $strOutput.Split(",")
# Get Horizontal and Vertical Length
$aSplitString2 = $aSplitString[0].Split("x")
$strHorizontal = $aSplitString2[0]
$strVertical = $aSplitString2[1]
$aSplitString2 = $null
#Get Color Depth and Frequency
$aSplitString2 = $aSplitString[1].Split(" ")
$strColour = $aSplitString2[1]
$strFrequency = $aSplitString2[4]
Run Code Online (Sandbox Code Playgroud)
不喜欢在一个字符串上使用这么多分割函数.我还能做什么?
我试图在上面的例子中将各个分辨率大小,颜色深度和频率放入它们的on变量中;
水平= 800垂直= 600颜色= 32频率= 60
假设我有一个像这样的 psobject :
$o=New-Object PSObject -Property @{"value"=0}
Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value {
echo "the square root of $($this.value) is $([Math]::Round([Math]::Sqrt($this.value),2))"
} -inputObject $o
Run Code Online (Sandbox Code Playgroud)
是否可以附加一个事件,以便在值属性更改时执行 Sqrt() 方法?IE :
PS>$o.value=9
Run Code Online (Sandbox Code Playgroud)
将产生
the square root of 9 is 3
Run Code Online (Sandbox Code Playgroud)
根据@Richard 的回答,这是有效的配方:
$o=New-Object PSObject -property @{"_val"=1}
Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value {
write-host "the square root of $($this._val) is $([Math]::Round([Math]::Sqrt($this._val),2))"
} -inputObject $o
Add-Member -MemberType ScriptProperty -Name 'val' -Value{ $this._val } -SecondValue { $this._val=$args[0];$this.Sqrt() } -inputObject $o
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 .xls 文件转换为 .xlxs 我尝试了此代码的许多变体,但每次遇到此错误消息时:
例外 lors de l'appel de « SaveAs » avec « 2 » 参数:« La méthode SaveAs de la classe Workbook a échoué。» Au caractère C:\temp\xlsx.ps1:18 : 6
- 试试{$opendoc.saveas($basename, $saveFormat)}
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullQualifiedErrorId : ComMethodTargetInvocation
这是我的代码:
$excel = new-object -comobject excel.application
$excel.Visible = $false
$saveFormat = "xlOpenXMLWorkbook";
ls c:\temp\*.xls | %{
$opendoc = $excel.workbooks.open($_.FullName)
$excel.DisplayAlerts =$false
$basename = $_.basename
try{
$opendoc.saveas($basename,$saveFormat,$null,$null,$false,$false,"xlNoChange","xlLocalSessionChanges")
# tried this one and got same error : $opendoc.saveas($basename, …Run Code Online (Sandbox Code Playgroud)