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

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."

Related

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

Executing SQL Script using OSQL do not return resultcode

I am executing some sql queries using OSQL through inno setup. I am using following code to run OSQL. This is just for example purpose
SQLQuery:= '"EXEC sp_addserver ''PCNAME'';"';
Param:= '-S(local) -Usa -Psa -Q ' + SQLQuery;
Exec('osql.exe', Param, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
This works fine. The problem is ResultCode value is always 0. Even if the query does not get executed. For example if I try same query like below where I pass in an invalid stored procedure name the ResultCode is still 0.
SQLQuery:= '"EXEC sp_invalidname ''PCNAME'';"';
Param:= '-S(local) -Usa -Psa -Q ' + SQLQuery;
Exec('osql.exe', Param, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
Why don't this return me a proper code. If I run the second query in management studio I get an error like this
Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'sp_invalidname'
Here return code is 2812. Why dont I get this when I run it through inno. What do I need to do to get this error code in inno?
Thanks to TLama, I updated my code as below and its working now. I had to add -b command line parameter and now it returns 1 if the command fails.
-b
Specifies that osql exits and returns a DOS ERRORLEVEL value when an error occurs. The value returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity of 11 or greater; otherwise, the value returned is 0. Microsoft MS-DOS batch files can test the value of DOS ERRORLEVEL and handle the error appropriately.
I updated my code as below.
Param:= '-S(local) -Usa -Psa -b -Q ' + SQLQuery;
Its explained in the documentation.

PowerShell calling function to change the computer machine name error

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?

Invalid command name in Tcl when using format

I'm running the following commands
set full_pin_name [format "%s %s" $top_cell $encoded_name]
puts "full_pin_name is $full_pin_name"
and I get the following error:
full_pin_name is invalid command name "A" B
when top_cell equals A and encoded_name equals B.
Why is this happening?
I suspect the problem is with your $top_cell variable, which has the value invalid command name "A". To check, try the following line before your format line:
puts ">$top_cell<"
If indeed, $top_cell value has a problem, you can then trace back to the last set command. Let us know if it fixes your problem, or we might try some other approaches.
Try repeating this in a plain Tcl shell, it works. Something is different in your Tcl shell.
Try
info body set
and
info body format
If either report something other than set isn't a procedure or format isn't a procedure then you have your culprit.
May be this code runs in a namespace which has the set command defined in it?
To demonstrate:
% namespace eval foo {
proc set args {
puts hey!
}
proc whatever {top_cell encoded_name} {
set full_pin_name [format "%s %s" $top_cell $encoded_name]
puts "full_pin_name is $full_pin_name"
}
}
% ::foo::whatever A B
hey!
can't read "full_pin_name": no such variable
%
As far as I can tell without a TCL interpreter here it should work assuming a normal interpreter with format defined. so it looks like maybe format has been renamed or doesn't exist as a command in the interpreter you are using?
To get you going you don't really need format to join strings anyway
set fill_pin_name "$top_cell $encoded_name"
should so what you need
Maybe your code looks like this:
set encoded_name B
if { [catch {[A]} top_cell]} {
set full_pin_name [format "%s %s" $top_cell $encoded_name]
puts "full_pin_name is $full_pin_name"
}
The result is:
full_pin_name is invalid command name "A" B

Resources