我有一个包含三个文件的文件夹,并希望dir /s /b在PowerShell中具有相同的功能.我怎么做?
例如,如果文件夹名称是temp3,它包含三个文本文件 - a.txt.b.txt和c.txt,做
C:\temp3> dir /s /b
Run Code Online (Sandbox Code Playgroud)
给我
C:\temp3\a.txt
C:\temp3\b.txt
C:\temp3\c.txt
Run Code Online (Sandbox Code Playgroud)
如何在PowerShell中获得相同的结果?
Powershell有两种方法可以将其他cmdlet导入会话,即Add-PsSnapIn和Import-Module.关于何时选择一个而不是另一个,文档并不清楚.有人知道这些之间的区别吗?
我需要获取文件路径名的最后一部分.
例:
c:\dir1\dir2\dir3\file.txt
Run Code Online (Sandbox Code Playgroud)
我需要将dir3放入变量中.
我一直在尝试分裂路径,但它给了我整条路径.
在制作简单的Web请求时,有没有办法告诉PowerShell环境只使用Internet Explorer的代理设置?
我的代理设置由网络策略(或脚本)控制,如果不必要,我不希望以后修改ps脚本.
更新: 来自参与者的精彩信息.我将用于此的最终脚本模板将如下所示:
$proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
$proxy = new-object System.Net.WebProxy
$proxy.Address = $proxyAddr
$proxy.useDefaultCredentials = $true
$url = "http://stackoverflow.com"
$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)
$str = [System.Text.Encoding]::ASCII.GetString($webpage)
Write-Host $str
Run Code Online (Sandbox Code Playgroud) 我想在这个脚本中的第二个函数调用抛出一个错误:
function Deploy
{
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$BuildName
)
Write-Host "Build name is: $BuildName"
}
Deploy "Build123"
Deploy #Currently prompts for input
Run Code Online (Sandbox Code Playgroud)
提示非常适合以交互方式使用脚本,但这也将由我们的构建服务器执行.
我最好的选择是用某种if东西做一些自定义验证吗?
我将凭据传递给Send-MailMessage命令时遇到了困难
这就是我正在运行的:
Send-MailMessage -smtpServer smtp.gmail.com -from 'myself@gmail.com' `
-to 'myself@gmail.com' -subject 'Test' -attachment C:\CDF.pdf
Run Code Online (Sandbox Code Playgroud)
它错误显示在消息下方,这显然是因为我没有通过我的gmail凭据
Send-MailMessage : The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first.
Run Code Online (Sandbox Code Playgroud)
我google了一下,也浏览了手册页,Send-MailMessage发现需要传递"-credential"参数.
我的问题是:怎么样?
我尝试使用Get-Credentials,如下所示:
$mycredentials = Get-Credential
Run Code Online (Sandbox Code Playgroud)
然后在弹出的框中输入我的用户名和密码.
然后我运行以下命令:
Send-MailMessage -smtpServer smtp.gmail.com -credentail $mycredentials `
-from 'myself@gmail.com' -to 'myself@gmail.com' -subject 'Test' -attachment C:\CDF.pdf
Run Code Online (Sandbox Code Playgroud)
并且它仍然以完全相同的错误失败.
所以我需要你们帮助我们如何将我的证书传递给Send-MailMessage命令.我了解了PScredentials,但不完全确定它是什么以及如何在这种情况下使用它.
我正在尝试创建一个JSON序列化的数组.当该数组只包含一个项时,我得到一个字符串,而不是一个字符串数组(在JSON中).
多个项目(按预期工作):
PS C:\> @("one", "two") | ConvertTo-JSON
[
"one",
"two"
]
Run Code Online (Sandbox Code Playgroud)
单项数组(不符合预期):
PS C:\> @("one") | ConvertTo-JSON
"one"
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我正在搜索一个简单的命令来查看服务器上登录的用户.我知道这个:
Get-WmiObject -Class win32_computersystem
Run Code Online (Sandbox Code Playgroud)
但这不会为我提供我需要的信息.它返回:域制造商模型名称(机器名称)PrimaryOwnerName TotalPhysicalMemory
我在Windows 2012服务器上运行Powershell 3.0.
也
Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select Antecedent -Unique
Run Code Online (Sandbox Code Playgroud)
没有给我我需要的确切答案.我很乐意看到空闲时间,或者他们是活跃还是离开.
powershell active-directory powershell-2.0 powershell-3.0 windows-server-2012
我正在创建一个PowerShell脚本,它将从用户输入组装HTTP路径.输出必须将用户输入中的任何空格转换为产品特定代码"%2F".
这是源和输出的示例:
站点URL可以是常量,但变量是更好的重用方法,因为程序中使用的是:/ http:%2F%2SPServer/Projects /"
$Company="Company"
$Product="Product"
$Project="The new project"
$SitePath="$SiteUrl/$Company/$Product/$Project"
Run Code Online (Sandbox Code Playgroud)
作为输出我需要:
'/http:%2F%2FSPServer%2FProjects%2FCompany%2FProductF2FThe%2Fnew%2Fproject'
Run Code Online (Sandbox Code Playgroud)