Need to set part of my PS string as a variable so when it runs it asks for the value - sql

I have tried using the below however what I am trying to achieve is being able to pass this through PowerShell and it asking me for a value for -companyCode.
"\Program Files (x86)\Home\Business Intelligence\ScriptoSql\"ScriptoSqldb1.exe -companyCode="" -sqlserver=SQLSERVER1\INS1,8071 -sqldatabase=StagingDB
Now the command works if I pass a value with it in the string but I want to make it dynamic so when it is called it will ask for that value and it is then entered by the user and the script runs.

Wrap the command invocation in a custom function with a mandatory parameter -CompanyCode. That way PowerShell will prompt for a missing parameter when the function is called.
function Invoke-ScriptToSqlDB {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$CompanyCode
)
& "${env:ProgramFiles(x86)}\...\ScriptoSqldb1.exe" -companyCode=$CompanyCode ...
}
Invoke-ScriptToSqlDB -CompanyCode 'whatever' # pass company code
Invoke-ScriptToSqlDB # prompt for company code

Related

How to use Subnetid parameter with New-AzContainerGroup Powershell Cmdlet?

I am testing creating an azure container instance group via an automation job which works, but in order to meet my requirements I need it to be attached to one of my configured VNETS, rather than using a public IP.
According to the documentation I need to create a hash table for the subnet ID, which I've attempted to do here, in the $hash variable
Name Value
---- -----
ManagedServices /subscriptions/{My-subscriptionID}/resourceGroups/{My-Resource-Group}/providers/Microsoft.Network/virtualNetworks/{My-VNET}/subnets/{My-Subnet}
However, whenever I run the command attempting to pass the $hash variable in, I receive the following error
New-AzContainerGroup : A parameter cannot be found that matches parameter name 'SubnetId'.
Here is the exact command that I am using
New-AzContainerGroup -ResourceGroupName Dev-Test -Name mycontainer -Image najarramsada/phpipamscanagent -OsType Linux -DnsNameLabel phpipamtest -SubnetId $hash
I am expecting this to run and create a container in my chosen subnet but instead I am getting the error indicating that the parameter doesn't exist. I am also not certain I created my hash table correctly as I've never used them before.
The error message you are receiving indicates that you have spelled the parameter incorrectly.
New-AzContainerGroup : A parameter cannot be found that matches parameter name 'SubbnetId'.
New-AzContainerGroup -ResourceGroupName Dev-Test -Name mycontainer -Image najarramsada/phpipamscanagent -OsType Linux -DnsNameLabel phpipamtest -SubnetId $hash
When creating your hashtable it looks as though you may need to use the following:
$hash = #{
Id = 'resourceId'
Name = 'FriendlyName'
}
Taken from the NOTES section here:
https://learn.microsoft.com/en-us/powershell/module/az.containerinstance/new-azcontainergroup?view=azps-7.3.2#notes

How to set variables to custom function in Jmeter?

I have a basic function in BeanShellSampler.bshrc at Jmeter 4.0
String getMyString(String strParam) {
return "MyString: "+strParam;
}
I called in BeanShell Sampler as below
String N = "123123";
log.info("${__BeanShell(getMyString("${__V(Var${N})}"),)}");
When I run Sampler output is somthing like that.
2018-06-18 15:25:40,080 INFO o.a.j.u.BeanShellTestElement: MyString: Var${N}
How can I set string variable to my function?
I read function articles in Jmeter web site
Thank you.
Add the next line to user.properties file:
beanshell.sampler.init=BeanShellSampler.bshrc
Amend your code to look like:
String N = "123123";
log.info(getMyString(N));
That's it, you should get MyString: 123123 in jmeter.log file
Be aware that starting from Jmeter 3.1 it is recommended to use Groovy for all forms of scripting as Groovy performance is much better comparing to Beanshell so consider taking the following steps instead:
Create a file, i.e. foo.groovy in "bin" folder of your JMeter installation and put your function there:
String getMyString(String strParam) {
return "MyString: " + strParam;
}
Add the next line to user.properties file:
You should be able to refer your custom code from __groovy() function like:
${__groovy(log.info(getMyString("123123")),)}
functions can be used anywhere in the Test Plan
For each BeanShell Program type there are different beanshell.*.init properties defined in bin/user.properties
beanshell.function.init=BeanShellFunction.bshrc
beanshell.preprocessor.init=BeanShellSampler.bshrc
beanshell.postprocessor.init=BeanShellSampler.bshrc
beanshell.assertion.init=BeanShellFunction.bshrc
Hence the same function which needs to be called from any program(preprocessor, postprocessor, etc) we need to copy the function to every .bshrc file OR use same .bshrc file for every program init property.
In your case if you are using local string variable N and passing it along with the script. If you use ${Variable} there must be a JMeter variable defined so that JMeter can pick its value. To do that you can use vars.put , write N value to JMeter variables and use ${N} .
I have defined Var123123 value as FinalValue as shown below
And 2 beanshell samplers one is to put String variable N to Jmeter variable and one is beanshell script as shown below
You can see in the log its printed VAR123123's value which is FinalValue
The reason why i took 2 beashell samplers is if i write N to JMeter variables and use it in same script its not updating N value until the sampler executed..
References :
Configuring JMeter
JMeter Beanshell
Please let me know if it helps

Specifying build parameters to FAKE

I am essentially asking for an update to date answer to the already answered question: Can I pass a parameter to a F# FAKE build script?
Here is build.fsx
let revisionNumber = getBuildParamOrDefault "rev" "123"
Target "Log" (fun _ ->
trace ("Revision Number: " + revisionNumber)
)
RunTargetOrDefault "Log"
Output running Fake.exe .\build.fsx:
Perfect!
Output running Fake.exe .\build.fsx rev=456 (as suggested by this answer: https://stackoverflow.com/a/26597179/2382536):
Starts with
But at the bottom gives the correct result:
What format do I need to pass the parameters in to get rid of the warning message?
Passing parameters is done using the --envvar parameter. Until recently you can just add parameters in a way you did after the build target but not anymore. I believe this was changed in order not to confuse build parameters with (optional) build target name.
So, try this:
fake build.fsx Push --envvar rev 456
I just want to share the link to the official documentation:
TLDR:
You can either use
--envvar [-ev] <name:string> <value:string>
to set a variable to a custom value, or
--envflag [-ef] <name:string>
to set a variable to true, or
--fsiargs --debug+ buildscript.fsx someArg1 anotherArg2
to pass all arguments (including build script name, importent!) direcly to fsi.exe

AHK: Manage multiple scripts

I got many scripts. I want to be able to manage them all in 1 in script.
What I want is that the main script will activate a certain script, then when the secondary script is done, it returns a value to the main script. After that, the main script calls another secondary script, etc...
Is there a proper way to do this?
More precise question:
Is it possible to activate a AHK script from another script AHK?
At the moment, to detect that at a secondary script is complete, the way I currently use is that right before the end of the secondary script, I press a combinaison of keys that the main script will detect. And once detected, it will increase a main script variable by one and this will trigger the activation of the next script. Is there a better way to achieve this?
The main script could call the other scripts using RunWait. The scripts could then communicate back before terminating themselves.
The best option for communication would be to use OnMessage.
The following is a working example from the documentation:
; Example: Send a string of any length from one script to another. This is a working example.
; To use it, save and run both of the following scripts then press Win+Space to show an
; InputBox that will prompt you to type in a string.
; Save the following script as "Receiver.ahk" then launch it:
#SingleInstance
OnMessage(0x4a, "Receive_WM_COPYDATA") ; 0x4a is WM_COPYDATA
return
Receive_WM_COPYDATA(wParam, lParam)
{
StringAddress := NumGet(lParam + 2*A_PtrSize) ; Retrieves the CopyDataStruct's lpData member.
CopyOfData := StrGet(StringAddress) ; Copy the string out of the structure.
; Show it with ToolTip vs. MsgBox so we can return in a timely fashion:
ToolTip %A_ScriptName%`nReceived the following string:`n%CopyOfData%
return true ; Returning 1 (true) is the traditional way to acknowledge this message.
}
; Save the following script as "Sender.ahk" then launch it. After that, press the Win+Space hotkey.
TargetScriptTitle = Receiver.ahk ahk_class AutoHotkey
#space:: ; Win+Space hotkey. Press it to show an InputBox for entry of a message string.
InputBox, StringToSend, Send text via WM_COPYDATA, Enter some text to Send:
if ErrorLevel ; User pressed the Cancel button.
return
result := Send_WM_COPYDATA(StringToSend, TargetScriptTitle)
if result = FAIL
MsgBox SendMessage failed. Does the following WinTitle exist?:`n%TargetScriptTitle%
else if result = 0
MsgBox Message sent but the target window responded with 0, which may mean it ignored it.
return
Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle) ; ByRef saves a little memory in this case.
; This function sends the specified string to the specified window and returns the reply.
; The reply is 1 if the target window processed the message, or 0 if it ignored it.
{
VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0) ; Set up the structure's memory area.
; First set the structure's cbData member to the size of the string, including its zero terminator:
SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
NumPut(SizeInBytes, CopyDataStruct, A_PtrSize) ; OS requires that this be done.
NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize) ; Set lpData to point to the string itself.
Prev_DetectHiddenWindows := A_DetectHiddenWindows
Prev_TitleMatchMode := A_TitleMatchMode
DetectHiddenWindows On
SetTitleMatchMode 2
SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle% ; 0x4a is WM_COPYDATA. Must use Send not Post.
DetectHiddenWindows %Prev_DetectHiddenWindows% ; Restore original setting for the caller.
SetTitleMatchMode %Prev_TitleMatchMode% ; Same.
return ErrorLevel ; Return SendMessage's reply back to our caller.
}
Well, I'm not sure why you'd want to make one script run another one... but here are a few other methods:
Include a script in another one
but, you know you can include a script inside another one, right? That is, you can use another scripts functions in your main script.
Make sure a particular script is loaded
"I got many scripts" too. Sometimes I need to make sure that a particular one is included before I can use it, so I put this at the top:
;make sure core.ahk is loaded since it is required
#include c:\ahk\core.ahk
And you don't have to worry about it getting included more than once (unless you need it) because:
#Include ensures that FileName is included only once, even if multiple inclusions are encountered for it. By contrast, #IncludeAgain allows
multiple inclusions of the same file, while being the same as #Include
in all other respects.
Now, when I include file.ahk in main.ahk, I am assured of no problems using the functions from core.ahk that file.ahk requires. And even if I include core.ahk again in main.ahk it is no worry (unless it contains subroutines instead of just functions - in which case they get run at the point where they were included, so it's best not to put subroutines in your ahk libraries).
Use good ole' RUN on Scripts
Aside from that, you know you can always use the run command to launch an ahk script. You don't have to do all that fancy WM_SENDMESSAGE stuff.
Communicate betweenst scripts using a hidden GUI
Another way for two scripts to communicate between each other is for script #1 to keep open a hidden GUI window that has an edit box and a submit button. This window will never be shown. Now, Script #2 hunts for that message box, uses send to put a string in the edit box, and then control-click to push the submit button. Now script #1 has just received input from script #2. And you don't even have to hunt for the window if you put the windows hwnd value in both scripts (so they already know it ahead of time). This works like a charm.
Tell if a script has completed
If you use ahk's run command, there is an parameter that will give you back the PID of that process (PID = Process ID). You can use this PID to check to see if the script is running, and you can use it to terminate the process.
Also, if you use runwait - the script using that command will pause and wait for the runn-ed process to complete and close before continuing.
theoretically you could also use a file object between the scripts as a sort of stdin/stdout method as when opening a file with the file object you can set it as shared.
You could also set an environment variable and pass the name of the variable to the script ,given that you have setup argument handling in the target script, which then sets the environment variable value on closing. using RunWait and this you could find out what the return result of the script is after it runs.
Lastly, look into using a function as that is probably the "best practice" for what you are trying to do. Since a function can do anything a script can do and you can pass it an array to operate on or with using ByRef on the array param. This would mean that you don't have to write in a bunch of parameters when writing the function and the variables would release memory once the function is complete, automatically. You can even write your functions in a separate file and use #Include to use them in your script.

SQL Powershell to Create Backup of Objects

I would like to create a powershell script that I can run to backup objects to file before updating them. My goal is to backup objects before changing them in case something breaks. I would like to pass in parameters to run like the following:
backupobjects.ps1 -servername -databasename -schemaname -objectname -outputdirectory
So if I call this powershell script and pass in parameters the script will connect to the database and find the object and save the CREATE script and save the object to the outputdirectory passed in and put BEFORE_objectname.sql as the filename.
I am just starting in powershell so accepting parameters I have not learned yet.
Any guidance or suggestions would be helpful.
Rather than write it for you, here are a couple of nudges:
1) param is how you pass in parameters in powershell. I like to do it like so:
param (
[string] $server = (Read-Host "Enter a server name"),
[string] $db = (Read-Host "Enter a database name")
)
you then reference $server and $db later in your script as though you'd explicitly initialized them.
2) Most (if not all) objects in SQL server have a Script() method attached to them. For instance take a look at the Table class.
3) You can control how objects are scripted using the ScriptingOptions class. When you invoke the Script() method on an object, pass a ScriptingOptions object as an argument and the scripting behavior will be governed it.