How do I use Powershell to return VM Name, TagName, TagValue in Azure? - azure-powershell

I'm not very good with parsing and displaying values in hash tables in PowerShell yet.
I want to use command to show the VM name, a specific Tag's name and the value of that tag for that VM.
Something like one of these (even though I know these commands don't work, it's what I'm trying to do):
Get-AzResource -Name SYSTEM* -ResourceType "Microsoft.Compute/virtualMachines" -TagName BuiltBy | Select Name,TagName,TagValue
Name TagName TagValue
---- ------- --------
SYSTEM1 BuiltBy Frank
SYSTEM2 BuiltBy Frank
SYSTEM3 BuiltBy Betty
SYSTEM4 BuiltBy Frank
Get-AzResource -Name SYSTEM* -ResourceType "Microsoft.Compute/virtualMachines" -TagName BuiltBy | Where {$.TagValue -eq 'Betty'} | Select Name,TagName,TagValue
Name TagName TagValue
---- ------- --------
SYSTEM3 BuiltBy Betty
Get-AzResource -Name SYSTEM* -ResourceType "Microsoft.Compute/virtualMachines" -TagName BuiltBy | Where {$.TagValue -ne 'Frank'} | Select Name,TagName,TagValue
Name TagName TagValue
---- ------- --------
SYSTEM3 BuiltBy Betty

Below is the PowerShell script for finding VM Name, Tag Name and Tag Value
$vmlist=Get-AzResource -ResourceType 'Microsoft.Compute/virtualmachines'
foreach( $item in $vmlist){ if($item.Tags.Keys -eq 'CreatedDate' -and $item.Name -eq 'shbhost'){
get-azvm -Name $item.Name |select -Property Name,Tags
}}
Output

Related

SQL saviong outputs to seperate files

I have a table like this:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255)
);
The Ouput:
**Person ID | Last Name**
100 | Jones
105 | Davids
109 | Jones Jr
110 | Jones Jr Jr
Is it possible that SQL can put the data in sperate CSV files? In this case 2 csv File which contains
CSV File1:
**Person ID | Last Name**
105 | Davids
and in the other csv 2 file:
**Person ID | Last Name**
100 | Jones
109 | Jones Jr
110 | Jones Jr Jr
If you are using SQL SERVER
$AttachmentPath = "CV File location"
$QueryFmt= "Query"
Invoke-Sqlcmd -ServerInstance Server -Database DBName -Query $QueryFmt | Export-CSV $AttachmentPath
If you are using Oracle 12, you can simply type
set markup csv on;
spool C:\DBA\test\file1.csv;
Select * from persons where name like 'David';
spool C:\DBA\test\File2.csv;
Select * from persons where name not like 'David';
Another way is to write a procedure and execute this procedure.

Concat multiple rows PSQL

id | name | Subject | Lectured_Times | Faculty
3258132 | Chris Smith | SATS1364 | 10 | Science
3258132 | Chris Smith | ECTS4605 | 9 | Engineering
How would I go about creating the following
3258132 Chris Smith SATS1364, 10, Science + ECTS4605, 9,Engineering
where the + is just a new line. Notice how after the '+'(new line) it doesnt concat the id,name
try
SELECT distinct concat(id,"name",string_agg(concat(subject, Lectured_Times , Faculty), chr(10)))
from tn
where id = 3258132
group by id;
As mentioned above string_agg is perfect solution for this.
select
id, name, string_agg(concat(subject, Lectured_Times, Faculty), '\n')
from table
group by id, name

Select Only Characters after First Character in String

Sample results before string manipulation:
COLUMN1 |
-----------
A343jsk |
------------
Jsdefss |
------------
Vdkekd |
------------
Nod |
------------
An |
------------
How do I achieve:
343jsk |
------------
sdefss |
-----------
dkekd |
-----------
od |
-----------
n |
-----------
I tried this:
SELECT
COLUMN1 - LEFT(COLUMN1,1)
FROM
Table
however this does not work. Obviously I can't use RIGHT because result strings are not all the same length.
Use SUBSTRING():
SELECT
SUBSTRING(COLUMN1, 2, LEN(COLUMN1) - 1)
FROM
Table
Alternatively, (as #David Faber pointed out) you could use:
SELECT
RIGHT(column1, LEN(column1) - 1)
FROM
Table
[I personally prefer the first form]

Oracle Sql; Find same combination of columnvalues in two or more rows

I need some help with my query. Been searching for ages but can`t come up with the right sql statement.
This my table DRAFT DFT (only 1 table, these are all the columns)
RowID|SID Number|Column C|RELS Number|Column E|Dr Number |Column G |
1------ | 23101----- |21-8-2014| 22234 ----- | UNR---------| 14243-----|2
2------ | 23101 ----- |22-8-2014| 22234 ----- | UNS---------| 14243 ---| 2
3------ | 23101------ |28-8-2014| 22232 ----- | FRE ---------| 14243 ---| 2
What I need is the following :
I need to select all the rows & Columns values of the table where the combination of the value in column SID Number and RELS Number are duplicate so what I must see is ony the following 2 rows:
1------ | 23101----- |21-8-2014| 22234 ----- | UNR---------| 14243-----|2
2------ | 23101 ----- |22-8-2014| 22234 ----- | UNS---------| 14243 ---| 2
AND NOT THIS:
1------ | 23101----- |21-8-2014| 22234 ----- | UNR---------| 14243-----|2
2------ | 23101 ----- |22-8-2014| 22234 ----- | UNS---------| 14243 ---| 2
**3------ | 23101------ |28-8-2014| 22232 ----- | FRE ---------| 14243 ---| 2**
because the combination of SID Number and RELS Number is not duplicate.
I know that the column value of Dr Number is the same in all 3 rows.
Does that matter for my sql statement?
This was my statement:
SELECT *
FROM DRAFT DFT
INNER JOIN (SELECT SID Number,RELS Number, COUNT(*) AS "TOTALCOUNT
FROM DRAFT DTF1GROUP BY SID Number,RELS Number
HAVING COUNT (*)>1
) B ON DTF.SID Number=B.SID Number AND DTF.RELS Number=B.RELS Number
Just use analytic functions:
select d.*
from (select d.*, count(*) over (partition by sid, rels) as cnt
from draft
) d
where cnt > 1;

sql query to print users who have different passwords and its count

I've a oracle table values as below.
UserName Password Application
============== ================ ===============
John cat gmail
John cat ldap
John dog yahoo
John dog fusion
Rick boat oracle
Rick mat gmail
Rick boat yahoo
Joe lilly gmail
Joe lilly yahoo
Joe lilly oracle
I want to query users who have differnt passwords and also its count. I want to have the following as a result.
John cat 2
John dog 2
Rick boat 2
Rick mat 1
SQL Fiddle
Query 1:
WITH password_counts AS (
SELECT DISTINCT
UserName,
Password,
COUNT( DISTINCT Password ) OVER ( PARTITION BY UserName ) AS number_of_passwords,
COUNT( Password ) OVER ( PARTITION BY UserName, Password ) AS count_per_password
FROM SecurityHole
)
SELECT UserName,
Password,
count_per_password
FROM password_counts
WHERE number_of_passwords > 1
ORDER BY UserName, Password
Results:
| USERNAME | PASSWORD | COUNT_PER_PASSWORD |
|----------|----------|--------------------|
| John | cat | 2 |
| John | dog | 2 |
| Rick | boat | 2 |
| Rick | mat | 1 |
The trick, of course, is knowing how to combine the two parts of the query:
SELECT user_password.UserName, user_password.Password, COUNT(*)
FROM user_password
JOIN (SELECT UserName
FROM user_password
GROUP BY UserName
HAVING COUNT(DISTINCT Password) > 1) multiple_passwords
ON multiple_passwords.UserName = user_password.UserName
GROUP BY user_password.UserName, user_password.Password
ORDER BY user_password.UserName
(Have a working SQL Fiddle demo!)
The sub-select (inner query) finds UserNames with more than one password (and gets unique rows, too). Outside of that, it's a standard "count of times entry is reused" query.
(And by the way, I hope these aren't real passwords... #shudders#)
this should work:
SELECT username,password,COUNT(*)
FROM t
WHERE username IN (SELECT username FROM t GROUP BY username HAVING COUNT(DISTINCT password>1))
GROUP by username,password
you can optimize it if you'll use with clause to prevent the table from being read twice.