我写了一个PS脚本,它将在IE中创建书签。在运行ps脚本之前,我需要执行以下命令。
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -force
如何避免在执行ps脚本之前在cmd以上执行?还是有人可以建议我将其转换为.exe或.bat文件?
如果“收藏夹”文件夹不存在,该脚本将失败,如何避免在此脚本中使用“收藏夹”文件夹。
我的PS脚本:-
function create-bookmarks{
$bookmarks = @{
"folder1" = @{
"Jenkins" = "https://jenkins.net";
};
"folder2" = @{
"Jenkins" = "https://jenkins.net";
};
}
foreach ($Name in $bookmarks.keys)
{
$IEFav = [Environment]::GetFolderPath('Favorites','None')
New-Item $IEFav\$Name -ItemType Directory -Force
$Shell = New-Object -ComObject WScript.Shell
$IEFav = Join-Path -Path $IEFav -ChildPath $Name
foreach ($key in $bookmarks[$Name].keys)
{
$FullPath = Join-Path -Path $IEFav -ChildPath "$($key).url"
$shortcut = $Shell.CreateShortcut($FullPath)
$shortcut.TargetPath = $bookmarks[$Name][$key]
$shortcut.Save()
}
}
}
create-bookmarks
如果c:/ users / username / Favorites下不存在Favorite文件夹,则会出错。
PS C:\Users\username\downloads\Bookmarks> .\Bookmarks.ps1
Cannot find an overload for "GetFolderPath" and the argument count: "2".
At C:\Users\username\downloads\Bookmarks\Bookmarks.ps1:27 char:45
+ $IEFav = [Environment]::GetFolderPath <<<< ('Favorites','None')
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 8/22/2018 6:44 PM DevOps
Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\username\downloads\Bookmarks\Bookmarks.ps1:30 char:31
+ $IEFav = Join-Path -Path <<<< $IEFav -ChildPath $Name
+ CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCom
mand