我可以删除多个文件夹中具有特定扩展名的文件:
Get-childitem * -include *.scc -recurse | remove-item
Run Code Online (Sandbox Code Playgroud)
但是我还需要删除具有特定名称的文件夹 - 特别是当你从subversion repo中下载文件时subversion创建的文件夹(".svn"或"_svn").
我正在尝试在PowerShell中构建文件路径,字符串连接似乎有点时髦.
我有一个文件夹列表:
c:\code\MyProj1
c:\code\MyProj2
Run Code Online (Sandbox Code Playgroud)
我想在这里获取DLL文件的路径:
c:\code\MyProj1\bin\debug\MyProj1.dll
c:\code\MyProj2\bin\debug\MyProj2.dll
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试做的事情:
$buildconfig = "Debug"
Get-ChildItem c:\code | % {
Write-Host $_.FullName + "\" + $buildconfig + "\" + $_ + ".dll"
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.我该如何解决?
由于PowerShell有许多类似SQL查询的cmdlet,是否有一种快速方法可以检查对象是否在Where-Objectcmdlet 的其他对象列表中?
像SQL中的东西:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
Run Code Online (Sandbox Code Playgroud)
当然我可以写一个简单的子程序,但我只是想知道是否有这样的功能.
我理解为什么IT管理员喜欢使用PowerShell,但开发人员是否应该每天使用它?
您是否经常在PowerShell中运行任何脚本以使您的生活更轻松?
我正在学习PowerShell.我可以将xml文件加载到变量中并对其进行操作.然后我可以调用对象的save方法来保存到磁盘.我希望有一种方法可以将生成的xml输出到屏幕.我好像找不到一个.有没有办法,除了输出到文件然后文件到屏幕?
我想读取XML文件并修改元素,然后将其保存回文件.在保留格式的同时保持匹配行终止符(CRLF与LF)的最佳方法是什么?
这是我所拥有的,但它没有这样做:
$xml = [xml]([System.IO.File]::ReadAllText($fileName))
$xml.PreserveWhitespace = $true
# Change some element
$xml.Save($fileName)
Run Code Online (Sandbox Code Playgroud)
问题是在我混合了LF和CRLF之后,删除了额外的新行(在xml中也称为空行).
感谢帮助一个powershell新手:)
我正在使用Powershell v 2.0.并将文件和目录从一个位置复制到另一个位置.我使用字符串[]来过滤掉文件类型,还需要过滤掉被复制的目录.正在过滤掉文件,但是,我尝试过滤的目录obj仍在被复制.
$exclude = @('*.cs', '*.csproj', '*.pdb', 'obj')
$items = Get-ChildItem $parentPath -Recurse -Exclude $exclude
foreach($item in $items)
{
$target = Join-Path $destinationPath $item.FullName.Substring($parentPath.length)
if( -not( $item.PSIsContainer -and (Test-Path($target))))
{
Copy-Item -Path $item.FullName -Destination $target
}
}
Run Code Online (Sandbox Code Playgroud)
我已经试过各种方法对其进行过滤,\obj 或*obj* 或\obj\ 但似乎没有任何工作.
谢谢你的帮助.
我正在尝试使用AzureResourceManager PowerShell模块来创建和配置网站.我开始使用Visual Studio生成的模板文件,当我使用它时,它可以正常工作New-AzureResourceGroup -TemplateFile website.json.
所以现在我正在尝试调整模板文件来配置站点.我正在尝试设置php和.NET Framework版本.根据模式,这些属性是通过资源数组中的配置对象设置的.
这是我的json模板的网站部分."资源"部分是我添加的内容:
{
"apiVersion": "2014-06-01",
"name": "[parameters('siteName')]",
"type": "Microsoft.Web/sites",
"location": "[parameters('siteLocation')]",
"tags": {
"[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource"
},
"dependsOn": [
"[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
],
"properties": {
"name": "[parameters('siteName')]",
"serverFarm": "[parameters('hostingPlanName')]"
},
"resources": [
{
"apiVersion": "2014-04-01",
"type": "Microsoft.Web/sites/config",
"name": "config",
"properties": {
"name": "config",
"phpVersion": "",
"netFrameworkVersion": "V4.5"
}
}
]
},
Run Code Online (Sandbox Code Playgroud)
当我通过此模板时,Test-AzureResourceGroupTemplate我收到此错误:
Code : InvalidTemplate
Message : Deployment template validation failed: 'The template resource 'config' for …Run Code Online (Sandbox Code Playgroud) 众所周知,这Write-Host是邪恶的.In PowerShell 5,Write-Information被添加并被认为是替换Write-Host.
但是,真的,哪个更好?
Write-Host因为它不使用管道是邪恶的,所以输入消息不能被重用.
但是,Write-Host只是在控制台中展示一些东西呢?在什么情况下我们应该重用输入?
无论如何,如果我们真的想重用输入,为什么不写这样的东西:
$foo = "Some message to be reused like saving to a file"
Write-Host $foo
$foo | Out-File -Path "D:\foo.log"
Run Code Online (Sandbox Code Playgroud)
另一个缺点Write-Host是,Write-Host可以通过使用-ForegroundColor和指定消息在控制台中显示的颜色-BackgroundColor.
另一方面,通过使用Write-Information,输入消息可以通过No.6管道在任何我们想要的地方使用.并且不需要像我上面写的那样编写额外的代码.但是黑暗的一面是,如果我们想要将消息写入控制台并保存到文件中,我们必须这样做:
# Always set the $InformationPreference variable to "Continue"
$InformationPreference = "Continue";
# if we don't want something like this:
# ======= Example 1 =======
# File Foo.ps1
$InformationPreference = "Continue";
Write-Information "Some …Run Code Online (Sandbox Code Playgroud) powershell ×10
azure ×1
chocolatey ×1
directory ×1
recursion ×1
scripting ×1
string ×1
windows ×1
windows-7 ×1
xml ×1