PowerShell: Remove Trailing "..." - string

I'm trying to create a powershell script that will get the list of scheduled tasks. I've got far enough to get a complete list of tasks, however I need to remove the trailing "..."
How do you do that?
$tasks | Select-String -pattern "Disabled" | ft #{Expression
={$_.Line};Label="Line";width=44}
Output:
Line
----
AD RMS Rights Policy Template Management ...
AD RMS Rights Policy Template Management ...
Proxy ...
UserTask ...
UserTask-Roam ...
Consolidator ...
KernelCeipTask ...
UsbCeip ...
ScheduledDefrag ...
Scheduled ...
Microsoft-Windows-DiskDiagnosticDataColl ...
Microsoft-Windows-DiskDiagnosticResolver ...
Notifications ...
WinSAT ...
ActivateWindowsSearch ...
ConfigureInternetTimeService ...
DispatchRecoveryTasks ...
ehDRMInit ...
InstallPlayReady ...
mcupdate ...
MediaCenterRecoveryTask ...
ObjectStoreRecoveryTask ...
OCURActivate ...
OCURDiscovery ...
PBDADiscovery ...
PBDADiscoveryW1 ...
PBDADiscoveryW2 ...
PeriodicScanRetry ...
PvrRecoveryTask ...
PvrScheduleTask ...
RecordingRestart ...
RegisterSearch ...
ReindexSearchRoot ...
SqlLiteRecoveryTask ...
UpdateRecordPath ...
CorruptionDetector ...
DecompressionFailureDetector ...
HotStart ...
LPRemove ...
SystemSoundsService ...
GatherNetworkInfo ...
Background Synchronization ...
Logon Synchronization ...
AnalyzeSystem ...
RacTask ...
RegIdleBackup ...
WindowsParentalControls ...
WindowsParentalControlsMigration ...
AutoWake ...
GadgetManager ...
SessionAgent ...
SystemDataProviders ...
SR ...
Interactive ...
IpAddressConflict1 ...
IpAddressConflict2 ...
MsCtfMonitor ...
SynchronizeTime ...
ResolutionHost ...
QueueReporting ...
BfeOnServiceStartTypeChange ...
UpdateLibrary ...
ConfigNotification ...
Calibration Loader ...

Don't use Format-Table (ft) since it will attempt to fit the data within the number of columns available in the console and you've limited the space for the "line" column to 44 chars. Try this instead:
$tasks | Select-String -pattern "Disabled" | Foreach {$_.Line}

$tasks | Select-String -pattern "Disabled" | ForEach-Object
{ $_ -replace "Disabled", ""} | ForEach-Object { $_ -replace "Could not start",
""} | ForEach-Object { $_.Trim() }
Does exactly as I wanted.

I'm not sure what $tasks has in it so I wrote this based off get-service but it should work in your case:
$tasks | Select-String -pattern "Disabled" | ft #{Expression=
{$intLength = 44; if($_.Line.length -lt $intLength) {$intLength=$_.Line.length} $_.Line.substring(0,$intLength)};Label="Line"}

Related

PowerShell to search for a negativ value and replace it

This is my data:
"Number";"Name";"Manufacturer";"Brand ID";"EAN";"Price";"Currency Code";"StockLevel"
"88212115";"Starter";"PowerMax";"4543";"5713852365634";"79,81";"EUR";"181"
"88212119";"Starter";"PowerMax";"4543";"5713852365672";"209,07";"EUR";"1"
"88212120";"Starter";"PowerMax";"4543";"5713852365689";"91,56";"EUR";"30"
"88212121";"Starter";"PowerMax";"4543";"5713852365696";"79,81";"EUR";"-6"
"88212125";"Starter";"PowerMax";"4543";"5713852365733";"84,51";"EUR";"39"
"88212128";"Starter";"PowerMax";"4543";"5713852365764";"108,59";"EUR";"23"
"88212130";"Starter";"PowerMax";"4543";"5713852365788";"133,86";"EUR";"1"
"88212135";"Starter";"PowerMax";"4543";"5713852365832";"58,66";"EUR";"-11"
"88212136";"Starter";"PowerMax";"4543";"5713852365849";"115,95";"EUR";"-23"
"88212138";"Starter";"PowerMax";"4543";"5713852365863";"99,49";"EUR";"8"
"88212140";"Starter";"PowerMax";"4543";"5713852365887";"66,59";"EUR";"6"
What I need is to have all negativ values i the StockLevel column (column 8) replaced with 0
In another question I had The Forth Bird provided me with this code that works fine
Get-Content "C:\Users\SvcBI\Documents\Autodoc\Autodoc_Temp.csv" | ForEach-Object {
if($_.Contains("MAHLE AM GmbH")) {
$_.Replace("4543", "287")
} else { $_ } } | Set-Content C:\Users\SvcBI\Documents\Autodoc\POWERMAX_DK_2.csv
So, I tried to rewrite it to this
Get-Content "O:\Lagertal_til_kunder\Autodoc\myTempFile.csv" | ForEach-Object {
if($_.Contains("-[0-9]*")) {
$_.Replace("-[0-9]*", "0")
}
else { $_ } } | Set-Content O:\Lagertal_til_kunder\Autodoc\POWERMAX_DK_2.csv
But it don't work.
All negativ values is still present in POWERMAX_DK_2.csv
Please help /Kim

how to make a policy to tag CreatedBy: AccountID?

i am trying to make a policy that tag each resource we create in our organization with the AccountID value.
i have two tags to force:
CreatedDate
CreatedBy
The "CreatedBy" tag name i cant find any idea how to insert the "AccountID" who created this resource in the tag value.
I saw when i create a kubernetes service that his resources have this TagName CreatedBy that pointing to the kubernetes .. which means that kind of solution already exist i guess.
my policy looks like this :
{
"mode": "All",
"policyRule": {
"if": {
"allOf": [
{
"field": "tags['CreatedOnDate']",
"exists": "false"
},
{
"field": "tags['CreatedBy']",
"exists": "false"
}
]
},
"then": {
"effect": "append",
"details": [
{
"field": "tags['CreatedOnDate']",
"value": "[utcNow()]"
},
{
"field": "tags['CreatedBy']",
"value": [????]
}
]
}
},
"parameters": {}
}
Here's a working example to automatically tag resources with creator's name.
here is my solution ...
i am earching for all resources with tag name = "created_By" = "None" (i taged all my resources like this)
than i get the caller id from the az-log if available, after i run this one time i make a schdule runbook to run everyday and tag the created resources of the last day.(of course dont forget to modify the date to- (Get-Date).AddDays(-1)
$resourceWithTag = Get-AzResource -Tag #{ created_By = "None" }
# Tag each resource with tag name 'created_By = None' with his CallerID'
foreach ($resource in $resourceWithTag) {
$users = Get-AzLog -ResourceId $resource.ResourceId -StartTime (Get-Date).AddDays(-90) -EndTime (Get-Date)| Select-Object Caller | Where-Object { $_.Caller } | Sort-Object -Property Caller -Unique | Sort-Object -Property Caller -Descending
if ((!$users) -or ($users.Caller -eq $null)) {
Write-Output "no logs"
}
else {
Update-AzTag -ResourceId $resource.ResourceId -Tag #{ created_By = $users[0]} -Operation Merge
}
}

How to retrieve multiple record-sets using 'npm mssql' in nodejs?

I have store-procedure which returns multiple results but when I retrieve it from npm mssql it returns the only first result.
in my T-SQL script:
CREATE PROCEDURE usp_myStoreProcedure #param1 varchar(3),#param2 varchar(3)
AS
BEGIN
select * from firstTable where name=#param1;
select * from secondTable where name=#param2;
END
when run this :
result1:
| Name | Subject | Mark|
|----------------------|
| Alice| Maths | 96 |
result2:
| Name | Subject | Mark|
|----------------------|
| Bob | Science | 93 |
in my nodejs using npm mssql package
let conn = await mssql.connect(config);
let output= await conn
.request()
.input("param1", mssql.VarChar(10), "Alcie")
.input("param2", mssql.VarChar(10), "Bob")
.execute("usp_myStoreProcedure");
mssql.close();
console.log(output);
current result:
{
"recordsets":
[
[
{
"Name": "Alice",
"Subject":"Maths"
"Mark": 96
}
],
[]
],
"recordset":
[
{
"Name": "Alice",
"Subject":"Maths"
"Mark": 96
}
],
"output": {},
"rowsAffected": [1,0],
"returnValue": 0
}
below result2 missing in the output:
| Name | Subject | Mark|
|----------------------|
| Bob | Science | 93 |

nodejs Dynamo DB document client. How to write filter expression for an attribute that exists with null data type

I have an attribute with null value. and some rows have boolean value. Now I want to write a filter expression to filter for the rows with attribute having null value. I am using dynamodb document client.
If you look at the docs, for a scan you can do:
ScanFilter: {
'<AttributeName>': {
ComparisonOperator: EQ | NE | IN | LE | LT | GE | GT | BETWEEN | NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH, /* required */
AttributeValueList: [
someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* more items */
]
},
/* '<AttributeName>': ... */
}
If you are using query:
QueryFilter: {
'<AttributeName>': {
ComparisonOperator: EQ | NE | IN | LE | LT | GE | GT | BETWEEN | NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH, /* required */
AttributeValueList: [
someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* more items */
]
},
/* '<AttributeName>': ... */
},
They both clearly support filtering by attributes with value null.

Iterate over a json using shell

I have a json in the following format. I want to iterate over this json file
{
"atest_engine": { "version": "96" },
"a_kdfvm": { "version": "68" },
"aseft_api": { "version": "" },
"push_psservice": { "version": "68" },
}
I tried jq utility and my script is as follows.
count=$( jq '. | length' test.json )
echo $count
for((i=0;i<$count;i++))
do
name=$(cat test.json | jq '.|keys['${i}']')
version=$(cat test.json | jq '.|keys['${i}'].version')
echo $name
echo $version
done
I am getting count and name properly but not able to fetch version information. How can I get it. I am new to scripting and any help in this regard is greatly appreciated.Thanks in Advance.
input.json
{
"atest_engine": { "version": "96" },
"a_kdfvm": { "version": "68" },
"aseft_api": { "version": "" },
"push_psservice": { "version": "68" }
}
command
jq -r 'to_entries[] | "\(.key)\t\(.value.version)"' input.json |
while read name version
do
echo "name:" $name
echo "version:" $version
done
result
name: atest_engine
version: 96
name: a_kdfvm
version: 68
name: aseft_api
version:
name: push_psservice
version: 68
First up your JSON example seems slightly malformed - the push_psservice line has a comma after it but this is most likely a typo.
You might find it easier to turn your object's fields into an array using jq's to_entries (see https://stackoverflow.com/a/24254365/4513656 ) e.g.:
to_entries | .[0].key
to_entries | .[0].value.version
Try this on https://jqplay.org/ .

Resources