代码如下:

param( [string] $stockCode )


if ($stockCode -eq ""){

Write-Host "请输入股票代码!"

exit

}

if($stockCode -match '^60*')

{

$stockCode= "sh" + $stockCode

}

elseif($stockCode -match '^00*')

{

$stockCode= "sz" + $stockCode

}

else {

Write-Host "输入错误,请重新输入!"

exit

}


while(1)

{

#以下为获取你输入的股票价格代码

$sourceURL="http://hq.sinajs.cn/list=$stockCode"

$wc=New-Object system.net.webclient

$content=$wc.downloadstring($sourceURL)

$str_splits=$content.Split(',');

$stockName=$str_splits[0].split('"')

Write-Host



$myStock=$stockName[1]

$lastPrice=$str_splits[2]

$nowPrice=$str_splits[3]

$higePrice=$str_splits[4]

$lowPrice= $str_splits[5]


$priceP=($nowPrice - $lastPrice )/$lastPrice


[double] $a="{0:0.####}" -f $priceP


$b="{0:p}" -f $a


if($lastPrice -gt $nowPrice )

{

$myColor="Green"

}

else

{

$myColor="Red"

}



#Write-Host -ForegroundColor $myColor "$myStock `t 当前价: $nowPrice `t`t 最高价:$higePrice `t 最低价:$lowPrice `t 昨收盘价:$lastPrice `t 涨跌幅:$b"

Write-Host -ForegroundColor $myColor "$myStock `t 当前价: $nowPrice `t`t 最高价:$higePrice `t 最低价:$lowPrice `t 涨跌幅:$b"

Write-Host


#以下为获取上证指数代码

$sourceURL="http://hq.sinajs.cn/list=s_sh000001"

$wc=New-Object system.net.webclient

$content=$wc.downloadstring($sourceURL)

$str_splits=$content.Split(',');

$nowPrice=$str_splits[1]

$ricePrice=$str_splits[2]

$ricePriceP=$str_splits[3]

$mon=$str_splits[5].Replace('";','')

if($ricePrice -match '^-')

{

$myColor="Green"

}

else

{

$myColor="Red"

}

Write-Host -ForegroundColor $myColor "上证指数 `t 最新: $nowPrice `t 涨跌: $ricePrice `t 涨跌幅:$ricePriceP `t 成交额(万):$mon"


#以下为获取深圳成指代码

$sourceURL="http://hq.sinajs.cn/list=s_sz399001"

$wc=New-Object system.net.webclient

$content=$wc.downloadstring($sourceURL)

$str_splits=$content.Split(',')

$nowPrice=$str_splits[1]

$ricePrice=$str_splits[2]

$ricePriceP=$str_splits[3]

$mon=$str_splits[5].Replace('";','')


if($ricePrice -match '^-')

{

$myColor="Green"

}

else

{

$myColor="Red"

}

Write-Host -ForegroundColor $myColor "深圳成指 `t 最新: $nowPrice `t 涨跌: $ricePrice `t 涨跌幅:$ricePriceP `t 成交额(万):$mon"



"="*88


Start-Sleep 60

}

==========================================================================