PowerShell calling function to change the computer machine name error - windows

Function ChangeName([string]$firstname, [string]$secondname)
{
net user administrator /active:yes
Write-Host "The new student machine name is: $firstname-S$secondname"
wmic computersystem where name="$(Get-WmiObject Win32_Computersystem).name" call rename name="$newname"
}
ChangeName "A" "B"
Error message:
invalid verb switch

Why not use the built-in Rename-Computer command?

Related

PowerShell DSC resource MSFT_PackageResource failed: The return code 1618 was not expected. Configuration is likely not correct

I have exe file downloaded in the VM in specific folder, I am trying to install Adobe using Powershell dsc code.
Script is failing with below error during execution (Configuration is called through ARM template), however if I check inside the VM, adobe is installed.
Tried running the same script manually inside the VM. Not facing any error though.
[{"code":"VMExtensionProvisioningError","message":"VM has reported a failure when processing extension 'configureWindowsServer'. Error message: "DSC Configuration 'Adobe' completed with error(s). Following are the first few: PowerShell DSC resource MSFT_PackageResource failed to execute Set-TargetResource functionality with error message: The return code 1618 was not expected. Configuration is likely not correct The SendConfigurationApply function did not succeed."\r\n\r\nMore information on troubleshooting is available at https://aka.ms/VMExtensionDSCWindowsTroubleshoot "}]}
Configuration Adobe
{
$PackagesFolder = "C:\Packages\Adobe"
$AcrobatReader = #{
"Name" = "Adobe Acrobat Reader DC"
"ProductId" = "XXXXXX-XXXXX-XXXXXXX"
"Installer" = "AcroRdrDC.exe"
"FileHash" = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"HashAlgorithm" = "SHA256"
"DestinationPath" = "$PackagesFolder\AdobeAcrobatReaderDC"
"Arguments" = "/msi EULA_ACCEPT=YES /qn"
}
Package AdobeAcrobatReaderDC {
Ensure = "Present"
Name = $AcrobatReader.Name
ProductId = $AcrobatReader.ProductId
Path = ("{0}\{1}" -f $AcrobatReader.DestinationPath, $AcrobatReader.Installer)
Arguments = $AcrobatReader.Arguments
}
}

Why power shell clear (or cls) command accepts a number as parameter?

From a Power Shell window, I can use the clear or cls as you all know.
But messing around I found that clear(5) or cls(5) works, also clear the screen (any number works, including negative numbers). But if I try something like clear(abc) or clear("abc") it throws error
abc: The term 'abc' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Why it accepts a number, if the number doesn't do anything (is not the number of lines to clear or anything), and why throws error with non-numerical?
Building on Santiago Squarzon's comment: the Clear-Host command is a function. You can see this by running:
Get-Command Clear-Host -OutVariable cmd
Output
CommandType Name
----------- ----
Function Clear-Host
Since functions are written in PowerShell, you can also view the contents (definition):
$cmd.Definition
Output (Windows PowerShell 5.1)
$RawUI = $Host.UI.RawUI
$RawUI.CursorPosition = #{X=0;Y=0}
$RawUI.SetBufferContents(
#{Top = -1; Bottom = -1; Right = -1; Left = -1},
#{Character = ' '; ForegroundColor = $rawui.ForegroundColor; BackgroundColor = $rawui.BackgroundColor})
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225747
# .ExternalHelp System.Management.Automation.dll-help.xml
Output (PowerShell 7.1.3, on Linux)
[Console]::Write((
& (Get-Command -CommandType Application clear | Select-Object -First 1).Definition
))
# .Link
# https://go.microsoft.com/fwlink/?LinkID=2096480
# .ExternalHelp System.Management.Automation.dll-help.xml
You can view about_Functions_Advanced to read more about this, but without [CmdletBinding()], it's not an "advanced" function and so it won't be validating its arguments in the same way.
Instead the function can access its unnamed arguments in $args but as you can see in the definition, it does not.
Get-Help Clear-Host will show you that it is not expecting any parameters:
NAME
Clear-Host
SYNOPSIS
SYNTAX
Clear-Host [<CommonParameters>]
DESCRIPTION
RELATED LINKS
https://go.microsoft.com/fwlink/?LinkID=225747
REMARKS
To see the examples, type: "get-help Clear-Host -examples".
For more information, type: "get-help Clear-Host -detailed".
For technical information, type: "get-help Clear-Host -full".
For online help, type: "get-help Clear-Host -online"
Even a non-advanced function that names parameters will have them show up in help output:
function Test-Thing($one, $two) {}
Get-Help Test-Thing
Output
NAME
Test-Thing
SYNTAX
Test-Thing [[-one] <Object>] [[-two] <Object>]
ALIASES
None
REMARKS
None

Cannot pass parameter with near call

I'm testing the Greeting contract in "workshop--exploring-assemblyscript-contracts" and try to pass parameter to saveMyMessage function but always get error. There maybe problem with quotations marks, I tried with " or '` but nothing succeeds. I'm running it on Windows.
This is the suggestion from the code:
near call greeting..testnet saveMyMessage '{"message": "bob? you in there?"}' --account-id .testnet
When I try
near call %ID% saveMyMessage '{"message": "bob? you in there?"}' --account-id %ID%
Or replace " with " or end single quote with ` always get error like this
Unknown argument: bob? you in there?}'
For starters, it appears you have a syntax error with 2 dots instead of 1 here - greeting..testnet
Have you tried seeing what %ID% is?
I would try the following syntax, but make sure when you set your IDenv variable, that it returns the correct value when you run echo $ID
Once you have your env variables set up correctly, try the following:
near call $ID saveMyMessage '{"message": "bob? you in there?"}' --account-id $ID

How to resolve error "String not recognized as valid DateTime"?

I have three date type variables passed from a batch file as strings. When they come over to the PS script, they are seen as:
20190710 112538
20190710 112538
20190710
The problem I am facing is that when I attempt to parse them, I receive an error stating that the string is not recognized as a valid DateTime.
I have tried changing the culture to and invariant one and no difference. I am sure I am missing something. Could I get some help with parsing these strings successfully?
Values Sent from here:
set YYYY=%DATE:~-4%
set MM=%DATE:~4,2%
set DD=%DATE:~7,2%
set HH=%time:~0,2%
set NN=%time:~3,2%
set SS=%time:~6,2%
set MS=%time:~9,2%
SET "_oStart=%YYYY%%MM%%DD% %HH%%NN%%SS%%MS%"
SET "_PSScript=C:\xx\xx.ps1"
SET "_oEnd=%YYYY%%MM%%DD% %HH%%NN%%SS%%MS%"
SET "_oDateRan=%YYYY%%MM%%DD%"
SET "_PSCMD=Powershell -ExecutionPolicy Bypass -File "%_PSScript%" -oStart "%_oStart%" -oEnd "%_oEnd%" -oDateRan "%_oDateRan%"
Param(
[String]$oStart,
[String]$oEnd,
[String]$oDateRan
)
[DateTime]$DateRan = [DateTime]::ParseExact($oDateRan, "yyyyMMdd", $null)
[DateTime]$StartTime = [DateTime]::ParseExact($oStart, "yyyyMMdd HHmmssff", $null)
[DateTime]$End = [DateTime]::ParseExact($oEnd, "yyyyMMdd HHmmssff", $null)
Error Message:
Exception calling "ParseExact" with "3" argument(s): "String was not
recognized as a valid DateTime."

how to use choice parameter in Jenkins pipeline in batch command

how to use choice parameter in Jenkins declarative pipeline in batch command.
I'm using following stage:
choice(
choices: 'apply\ndestroy\n',
description: '',
name: 'DESTROY_OR_APPLY')
stage ('temp') {
steps {
echo "type ${params.DESTROY_OR_APPLY}"
bat'echo "type01 ${params.DESTROY_OR_APPLY}"'
bat'echo "type01 %{params.DESTROY_OR_APPLY}%"'
bat'echo type01 [${params.DESTROY_OR_APPLY}]'
}
echo does resolve to correct parameter value but under bat none of the above code works.
You almost got the syntax right.
If you change it to one of the below options, the bat command receives the value of your choice.
steps {
bat "echo type01 ${DESTROY_OR_APPLY}"
}
or
steps {
bat 'echo type01 ' + DESTROY_OR_APPLY
}
You can also use ${params.DESTROY_OR_APPLY} in the first or params.DESTROY_OR_APPLY in the second example if you want to use the params definition consequently in your code.

Resources