java n00b在这里.
我有一个自定义数据类型定义如下:
public class MyData{
int value;
int type;
String name;
public MyData(int newValue, int newType, string newName)
{
value = newValue;
type = newType;
name = newName;
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用这个类的实例时,我希望它计算value属性,如下所示:
myData dataInstance = new myData(100,3,"customData");
System.out.println(dataInstance); // should print "100"
Run Code Online (Sandbox Code Playgroud)
这可以实现吗?
我编写了以下代码将csv转换为小写.
(Get-Content "$file" -Raw).ToLower() | Out-File "$outfile"
Run Code Online (Sandbox Code Playgroud)
但得到错误就像:
Get-Content : A parameter cannot be found that matches parameter name 'Raw'.
Run Code Online (Sandbox Code Playgroud)
我有一个 sample.txt 文件,我想在其中删除内容,除了第一行。
我的 sample.txt 文件如下所示:
1.dslkjfladsdjgmvjfgmldskbm;sldkvmg,;sdlmg;msj,;sdrl
2.dlkjfsadfjmsal;jsalv;dsvmdsfkgmrg,pvtpvhtphb[h.[y
3.fljsafckg,ksd,v;vyklt;vlkbmyulnmktr,ouf,f/.n,
4.dflcajsmglsdv;ks'ítb;pjk.'gpjnuk. uo.hulmk,vgjhumk.'l
Run Code Online (Sandbox Code Playgroud)
我希望输出结果如下所示:
1.dslkjfladsdjgmvjfgmldskbm;sldkvmg,;sdlmg;msj,;sdrl
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这些命令,但输出会清除整个文件。
Clear-Content C:\sample.txt | where { $_.length -gt 2}| Set-Content C:\sample.txt
Clear-Content C:\sample.txt | Select -first 2 | Set-Content C:\sample.txt
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我错过了什么吗?谢谢
我正在使用以下内容将时间字符串解析为TimeSpan:
string[] formats = { "hhmm", "hmm", @"hh\:mm", @"h\:mm\:ss", @"h\:mm", "hh:mm tt" };
parseSuccess = TimeSpan.TryParseExact(value, formats, CultureInfo.CurrentCulture, TimeSpanStyles.None, out dtValue);
Run Code Online (Sandbox Code Playgroud)
当我尝试解析像上午9:00,下午5:00这样的值时,返回false.这有什么不对?
我正在尝试学习编程/脚本编写的效率,并且我知道在后台发生的事情我可能不知道。
$sb = new-object System.text.stringbuilder;
$sb.append("Hello World");
Write-Host $sb.clear().append("Hello World 2");
Run Code Online (Sandbox Code Playgroud)
这是有效的还是它在后台执行.toString() ?
和
for (iteration) {
$temp = "test";
$temp = $null # Which is better?
# or
clear-variable temp # Which is better?
$temp = "test2"
[GC]::Collect(); # Is this needed?
}
Run Code Online (Sandbox Code Playgroud) 不确定我在这里做错了什么,我知道这与它位于 ForEach 循环内这一事实有关。我尝试移动下面的代码。即使代码各半(一半在循环外,一半在循环外似乎不起作用)。对于 PowerShell 来说还是个新手,我知道每次迭代我都需要将变量添加到 $table 中,或者将它们存储在某个位置并稍后读取它们。
foreach ($gp in $GPINFO) {
# Code that gets the values for $gp, $aclBefore and $aclAfter is here
$table = new-object psobject -Property @{
GPO_Name = $gp.DisplayName
Old_Owner = $aclBefore
New_Owner = $aclAfter
}
$table | Format-Table GPO_Name,Old_Owner,New_Owner
}
Run Code Online (Sandbox Code Playgroud)
如果你能帮助我找出我做错了什么,那就太好了,我知道每次 ForEach 从 $GPINFO 中获取 $gp 时,它都会运行 $table 的东西,这就是我的问题。因此,我最终得到的是多个表,每个表中包含一组数据,而不是一个连续的表。
提前致谢
我正在使用字符串比较在 powershell 中尝试 if else 条件。我按照文档使用 -eq 运算符进行了尝试。但低于错误。这里“Build.Reason”是一个预定义的变量。不知道为什么要为变量寻找 cmdlet 名称。
Write-Host "$(Build.Reason)"
if ($(Build.Reason) -eq "Manual" ) {
$temp = "https://url/api/qualitygates/project_status?&pullRequest=$(Build.Reason)"
Write-Host "Manual"
} else {
Write-Host "CI"
}
Run Code Online (Sandbox Code Playgroud)
错误
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\d7af16d6-ce3e-4dec-a636-9447962fdac4.ps1'"
Manual
Manual : The term 'Manual' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用 Powershell,已经遇到了问题。\n我正在使用 OpenWeathermap ( https://openweathermap.org/ ) 的 API 来创建类似天气机器人的东西。
\n我正在使用 API 中的这个函数:
\nWrite-WeatherCurrent -City $place -ApiKey $ApiKey -Units metric\nRun Code Online (Sandbox Code Playgroud)\n输出是这样的(如果我填写变量):\n10.2\xc2\xb0C (\xe2\x98\x81\xef\xb8\x8f 几朵云) in london
\n所以我希望这个输出保存在文件中。我已经尝试过使用命令输出文件和>>。但它仅在终端中输出并且文件为空。我不确定,但是是因为“Write”-WeatherCurrent吗?
\n如果有人能帮助我,我会很高兴:D
\n谢谢
\n我想要的是 get-help 为我的 cmdlet 输出以下内容
SYNTAX
Get-Somehting -A <Object> [-Package <Object>] [<CommonParameters>]
Get-Somehting -A <Object> [-Names <string[]>] [<CommonParameters>]
Get-Somehting -B <Object> [-Package <Object>] [<CommonParameters>]
Get-Somehting -B <Object> [-Names <string[]>] [<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)
下列
Function Get-Somehting{
[CmdletBinding()]
Param(
[Parameter(Mandatory,
ParameterSetName='A')]
[System.Object]$A,
[Parameter(Mandatory,
ParameterSetName='B')]
[System.Object]$B,
[Parameter(Mandatory,
ParameterSetName='package')]
[System.Object]$Package,
[Parameter(Mandatory,
ParameterSetName='Names')]
[String[]]$Names)
Process{
}
}
Run Code Online (Sandbox Code Playgroud)
给我
SYNTAX
Get-Somehting -A <Object> [-Package <Object>] [-Names <string[]>] [<CommonParameters>]
Get-Somehting -B <Object> [-Package <Object>] [-Names <string[]>] [<CommonParameters>]
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Windows Server 2016 上安装 docker。每当我FindContainerImage在 powershell 上运行命令时,它都会显示:
WARNING: Cannot bind the argument to parameter 'fastPackageReference' because it is an empty string.
WARNING: The property 'version' cannot be found on this object. Verify that the property exists.
WARNING: The property 'Name" cannot be found on this object. Verify that the property exists.
WARNING: Cannot find path 'C:\Users\[username]\AppData\Local\ContainerImage\ContainerImageGallery_ContainerImageSearchIndex.txt' because it does not exists.
WARNING: Http.Getsync did not return successful status code. Status Code: NotFound, The specified resource does not …Run Code Online (Sandbox Code Playgroud)