我正在尝试编写PowerShell脚本来停止本地计算机上的服务.
当我运行get-service cmdlet时,它按预期工作.当我使用stop-service cmdlet时,它会错误地指出不存在具有我指定名称的服务.如果确实如此,get-service cmdlet肯定会抛出相同的错误.
Get-Service "Service 1" # Returns Service and status
Stop-Service "Service 1" # Throws error below:
Stop-Service : Service 'Service 1' cannot be stopped due to the following error: Cannot open Service 1 service on computer '.'.
Run Code Online (Sandbox Code Playgroud)
使用stop-service cmdlet时有诀窍吗?
我正在寻找一种方法来使用其服务名称而不是进程名称或PID来终止Windows服务.两个显而易见的选择是pskill或taskkill,但我似乎找不到使用这些方法中的任何一种来按名称终止服务的方法.
可以通过服务名称来实现吗?如果是这样,有人能够提供一个快速的例子吗?
我正在完成 AngularJS 应用程序的基本设置(刚刚开始),目前正在尝试为该应用程序安装 Bower 组件。
当我bower install从终端运行时,出现以下错误:
Error: Cannot find module 'js-yaml'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (Path\bower\node_modules\configstore\index.js:9:12)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
Run Code Online (Sandbox Code Playgroud)
我没有js-yaml在我的脚本中的任何地方指定模块(据我所知),所以不确定这个依赖项来自哪里。
我尝试删除 node_modules 文件夹,清除 npm 缓存,包括js-yaml在package.json文件中,然后重新运行 npm install,但在运行时出现相同的错误bower install
如果有帮助,我的bower.json和package.json文件如下所示:
Bower.json
{
"name": "starter-node-angular",
"version": "1.0.0",
"dependencies": {
"bootstrap": "latest",
"font-awesome": "latest", …Run Code Online (Sandbox Code Playgroud) 我有一个链接到Item表的Images表(一个Item可以有很多Images).它们由Item Id链接.
我想在详细信息页面上返回项目的所有图像.我一直在尝试使用Viewbag来实现这个目标(下面).
控制器:
ViewBag.imageFile = (from f in storeDB.Images
where f.ItemId == id
select f.ImageURL).ToList();
Run Code Online (Sandbox Code Playgroud)
视图:
@foreach (var image in ((List<string>)ViewBag.imageFile))
{
@ViewBag.imageFile
}
Run Code Online (Sandbox Code Playgroud)
以上将为每个ImageUrl返回'System.Collections.Generic.List`1 [System.String]'.我认为问题是对象是返回而不是实际的List项字符串值,但无法弄清楚如何将其转换为我需要的字符串.计划是编辑此foreach以为每个URL创建img链接.
我也有以下Viewbag(测试看我能够至少获得Urls):
ViewBag.imageURL = string.Join(",", ViewBag.imageFile);
Run Code Online (Sandbox Code Playgroud)
以上内容返回正确的URL,但它们都在一个以逗号分隔的字符串中.
感谢任何帮助.
我目前有以下代码行.
(Get-Content 'file.txt') |
ForEach-Object {$_ -replace '"', ''} |
Set-Content 'file.txt'
Run Code Online (Sandbox Code Playgroud)
这在测试时起作用,但现在我试图在真实数据文件(13 GB)上使用它,并且这个使用Get-Content的过程导致Powershell消耗大量RAM并最终消耗机器上的所有可用RAM.
有没有更好的方法可以在没有相同数量的开销的情况下实现相同的结果?
似乎我正在做最佳实践的反面但不确定还有什么比上面更清洁/更少的RAM密集.
我想在许多文件中查找并替换某个字符串.其中一些文件可能比较大,所以我使用的StreamReader是System.IO命名空间中的类.
我遇到的问题是我不想将新值写入新文件(这是我目前所拥有的).我想只是"更新"当前文件.
$currentValue = "B";
$newValue = "A";
# Loop through all of the directories and update with new details.
foreach ($file in $Directories) {
$streamReader = New-Object System.IO.StreamReader -Arg "$file"
$streamWriter = [System.IO.StreamWriter] "$file"
# Switching $streamWriter to the below works.
# $streamWriter = [System.IO.StreamWriter] "C:\Temp\newFile.txt"
while($line = $streamReader.ReadLine()){
# Write-Output "Current line value is $line";
$s = $line -replace "$currentValue", "$newValue"
# Write-Output "The new line value is $s"
$streamWriter.WriteLine($s);
}
# Close the …Run Code Online (Sandbox Code Playgroud) powershell ×3
angularjs ×1
asp.net-mvc ×1
batch-file ×1
bower ×1
c# ×1
node.js ×1
npm ×1
windows ×1